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.
~n or ~% prints a newline character (which is equivalent to \n in a literal format string)
Begin takes an arbitrary number of expressions and executes each one of them but only returns the result of the last expression in the body.
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"
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With