Search code examples
formatracket

How to format output using racket


How do I format output using racket? I want to output a fixed-width number and fill it with 0 if the width is too small? How can I do it? I have searched the racket documentation but I can only find fprintf, which seems to be unable to do it.


Solution

  • You can use functions from the racket/format module. For example ~a:

    #lang racket
    (require racket/format)
    (~a 42 
        #:align 'right
        #:width 4
        #:pad-string "0")
    

    returns

    "0042"