How to format output using racket
Asked Answered
B

2

7

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.

Bedim answered 29/9, 2015 at 8:35 Comment(0)
C
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"
Certiorari answered 29/9, 2015 at 12:56 Comment(0)
E
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" 
Excitor answered 29/9, 2015 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.