Next: , Previous: , Up: formations   [Index]


52.5 Escape sequences for integers

Format Escape Sequence: ~d
Format Escape Sequence: ~x
Format Escape Sequence: ~o
Format Escape Sequence: ~b

Integer. Parameters: minwidth, padchar, commachar, commawidth.

Output an integer argument as a decimal, hexadecimal, octal or binary integer (respectively).

(format #t "~d" 123) -| 123

If the output is less than the minwidth parameter (default no minimum), it’s padded on the left with the padchar parameter (default space).

(format #t "~5,'*d" 12)   -| ***12
(format #t "~5,'0d" 12)   -| 00012
(format #t "~3d"    1234) -| 1234

The @ modifier causes a + sign to be prepended to positive numbers, zero included.

(format #t "~@b" 12) -| +1100
(format #t "~@d" 0)  -| +0

The : modifier adds a commachar (default comma) every commawidth digits (default 3).

(format #t "~:d" 1234567)         -| 1,234,567
(format #t "~10,'*,'/,2:d" 12345) -| ***1/23/45

Hexadecimal ~x output is in lower case, but the ~( and ~) case conversion directives described elsewhere can be used to get upper case.

(format #t "~x"       65261) -| feed
(format #t "~:@(~x~)" 65261) -| FEED
Format Escape Sequence: ~r

Integer in words, roman numerals, or a specified radix. Parameters: radix, minwidth, padchar, commachar, commawidth.

With no parameters output is in words as a cardinal like “ten”, or ~:r prints an ordinal like “tenth”.

(format #t "~r" 9)  -| nine        ;; cardinal
(format #t "~r" -9) -| minus nine  ;; cardinal
(format #t "~:r" 9) -| ninth       ;; ordinal

And also with no parameters, ~@r gives roman numerals and ~:@r gives old roman numerals. In old roman numerals there’s no “subtraction”, so 9 is VIIII instead of IX. In both cases only positive numbers can be output.

(format #t "~@r" 89)  -| LXXXIX     ;; roman
(format #t "~:@r" 89) -| LXXXVIIII  ;; old roman

When a parameter is given it means numeric output in the specified radix (which can be any integer, not only 2, 8, 10 or 16). The modifiers and parameters following the radix are the same as described for ~d above.

(format #f "~3r" 27)   ⇒ "1000"    ;; base 3
(format #f "~3,5r" 26) ⇒ "  222"   ;; base 3 width 5

Next: , Previous: , Up: formations   [Index]