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.
How to format output using racket
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"
© 2022 - 2024 — McMap. All rights reserved.