FORMAT cheat sheet
This is a mirror of http://www.celebris.net/~danlei/linux-bsd/format-strings.html (taking from the snapshot archived on 2008-03-27). Enjoy!

Common Lisp Format Control Strings

Overview of the Common Lisp format function’s control strings. Mostly taken from the Common Lisp HyperSpec, reduced to the examples.

Basic Output

~c Character

(format nil "~c" #\A) => "A" (format nil "~c" #\Space) => " " (format nil "~:c" #\Space) => "Space" (format nil "~@c" #\Space) => "#\\ " (format nil "~@:c" #\Control-Partial) => "Control-<PARTIAL> (Top-F)"

~% Newline

(format nil "~%") => "\n" (format nil "~2%") => "\n\n"

~& Fresh-Line

Outputs a newline if the output stream is not at the beginning of a line. ~n& produces ~&~(n-1)%.

(format nil "~&") => "" (format nil "a~&b") => "a\nb" (format nil "~2&") => "\n" (format nil "a~2&b") => "a\n\nb"

~| Page

(format nil "~|") => "^L" (format nil "~2|") => "^L^L") => "^L^L"

~~ Tilde

(format nil "~~") => "~" (format nil "~2~") => "~~"

Radix Control

~r Radix

~nr prints arg in radix n. The modifier flags and any remaining parameters are used as for the ~d directive. The full form is ~radix,mincol,padchar,commachar,comma-intervalr. If no prefix arguments are given, the argument should be an integer and is printed as an English word or Roman numeral.

(format nil "~2r" 10) => "1010" (format nil "~16r" 10) => "A" (format nil "~r" 4) => "four" (format nil "~:r" 4) => "fourth" (format nil "~@r" 4) => "IV" (format nil "~@:r" 4) => "IIII"

~d Decimal

The most general form of ~d is ~mincol,padchar,commachar,comma-intervald. If arg is not an integer, it is printed in ~a format and decimal base.

(format nil "~d" 42) => "42" (format nil "~@d" 42) => "+42" (format nil "~:d" 123456789) => "123,456,789" (format nil "~4d" 42) => " 42" (format nil "~4,'#d" 42) => "##42" (format nil "~,,'|:d" 123456789) => "123|456|789" (format nil "~,,,4:d" 123456789) => "1,2345,6789" (format nil "~d" 1.23) => "1.23"

~b Binary

Just like ~d but prints in radix 2.

(format nil "~b" 10) => "1010"

~o Octal

Just like ~d but prints in radix 8.

(format nil "~o" 10) => "12"

~x Hexadecimal

Just like ~d but prints in radix 16.

(format nil "~x" 10) => "A"

Also see this FORMAT summary table.


document, programming tips