Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format output using racket

Tags:

format

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.

like image 973
tian tong Avatar asked Sep 29 '15 08:09

tian tong


People also ask

How do you print a new line in a racquet?

~n or ~% prints a newline character (which is equivalent to \n in a literal format string)

What is start in DR racket?

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.


2 Answers

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"
like image 192
Greg Hendershott Avatar answered Nov 15 '22 09:11

Greg Hendershott


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" 
like image 20
Sylwester Avatar answered Nov 15 '22 10:11

Sylwester