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.
Asked
Active
Viewed 1,846 times
2 Answers
12
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"

Greg Hendershott
- 16,100
- 6
- 36
- 53
4
format
in #!racket
isn't as rich as sprintf
in C languages. A workaroundwould eb to do it yourself:
(require srfi/13)
(string-pad (number->string 23) 4 #\0) ; ==> "0023"

Sylwester
- 47,942
- 4
- 47
- 79