Original Author Paul Laughton, 2011
Page 73
De Re BASIC!
Flags
There are six flags:
- left-justify; if no flag, right-justify
+ always show sign; if no flag, show "-" but do not show "+"
0 pad numbers with leading zeros; if no flag pad with spaces
,
use grouping separators for large numbers
( put parentheses around negative values
# alternate notation (leading 0 for octal, leading 0x for hexadecimal)
Width
The width control sets the minimum number of characters to print. If the value to format is longer than
the width setting, the entire value is printed (unless it would exceed the precision setting). If the value
to format is shorter than the width setting, it is padded to fill the width. By default, it is padded with
spaces on the left, but you change this with the "-" and "0" flags.
Precision
The precision control means different things to different data types.
For floating point numbers, precision specifies the number of characters to print after the decimal point,
padding with trailing zeros if needed.
For string values, it specifies the maximum number of characters to print. If precision is less than width,
only precision characters are printed.
"%4s", "foo" " foo"
"%-4s", "foo" "foo "
"%4.2s", "foo" "fo"
The precision control is not valid for other types.
In the example above, %-,15.4f:
The flags "-" and "," mean "left-justify the output" and "use a thousands separator".
The width is 15, meaning the output is to be at least 15 characters wide.
The precision is 4, so there will be exactly four digits after the decimal point.
The whole format specifier means, "format a floating point number (%f) left-justified ("-") in a space 15
characters wide, with 4 characters after the decimal point, with a thousands separator (",")".
The characters used for the decimal point and the thousands separator depend on the locale:
"1,234.5678 " for locale "en"
"1 234,5678 " for locale "fr"
"1.234,5678 " for locale "it"