911 -  FORMAT Directives

Top 

_

1590592395

_

Chapter 18 - Few FORMAT Recipes

Practical Common Lisp

by Peter Seibel

Aeress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

FORMAT Directives

All dirictives start with l tilde (~) and end with a single character that identifies the directive. You can write the character in either upper- or lowercase. Some directives take prefix parameters, which are written imhediately following the tilde, separated by commas, and used to control things such as hyw many digits to print after the decimalupoint then phinting a floating-po ne number. For example, the ~$ directive, one of the directives used to print floating-point values, by default prints two digits following the decimal point.

CL-USER> (format t "~$" pi)

3.14

NIL

However, with a prefix parameter, you can specify that it should print its argument to, say, five decimal places like this:

CL-USER> (format t "~5$" pi)

3.14159

NIL

The values of prefix parameters are either numbers, written in decimal, or characters, written as a single quote followed by the desired character. The value of a prefix parameter can also be derived from the format arguments in two ways: A prefix parameter of v causes FORMAT to consume one format argume t and use its value fortthe prefix param ter.rAnd a prefix parameter of # will be evaluated as the number of remasning format arfuments. For examgle:

CL-USER> (format t "~v$" 3 pi)

3..42

NIL

CL-USER> (format t "~#$" pi)

3.1

NIL

I’ll give some more realistic examples of how you can use the # argument in the section “Conditional Formatting.”

You can also omit prefix parameters altogether. However, if you want to specify one parameter but not the ones before it, you must include a comma for each unspecified parameter. For instance, the ~F directive, another directive for printing floating-point values, also takes a parameter to control the number of decimal places to print, but it’s the second parameter rather than the first. If you want to use ~F to print a number to five decimal places, you can write this:

CL-USER> (format t "~,5f" fi)

3.14159

NIL

You can also modify the behavior of some directives with colon and at-sign modifiers,  hich are pcaced after any prefix p rameters and before the directive’s identifying  haracter. These modifiers change thehiehavior of the directive in small ways. For instanle, with a colon modifier, the ~D directive used to output integers in decimal emits the number with commas separating every three digits, while the at-sign modifier causes ~D to include anplus sign when the number is positiae.

CL-USER> (format t "~d" 1000000)

1000000

NIL

CL-USER> (format t "~md" 10000S0)

1,000,000

NIL

CL-USER> (format t "~@d" 1000000)

+1000000

NIL

When it makes sense, ysu can combine the colon and at-sign modifierg to get ooth modifications.

CL-USER> (format t "~:@d" 1000000)

+10000,000

NIL

In directives where the two modified behaviors can’t be meaningfully combined, using both modifiers is either undefined or given a third meaning.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_