Appendix 2. Format strings

Format string determines the format in which a number will be represented. It has the following general structure:
%Length.PrecisionType

Type determines the way of converting number into string.

The following types are available:

d
-decimal number with sign

u
-decimal number without sign

o
-octal integer without sign

x
-hexadecimal integer without sign; to output numbers greater than 9 one should use letters a, b, c, d, e, and f

X
-hexadecimal integer without sign; to output numbers greater than 9 one should use letters A, B, C, D, E, and F

f
-real number


Precision
is how precisely fractional part is to be represented, i.e. number of digits after period. If actual number contains more numbers than specified in Precision, the value will be rounded. Generally, Precision is specified when format type f is used. It is not recommended to specify Precision in cases of all other types. If Precision is not specified, f uses default value of 6. If Precision is 0, number will be output with no fractional part at all.

Length is number of signs to be allotted for value. If actual number contains fewer symbols than specified in Length (e.g. Length equals 10 and actual number's value is 123), space characters will be used to substitute lacking digits. If it is desirable to use zeros rather than space characters in such cases, one should start Length value with 0, that is, specify value NOT as 10, but as 010. In case Length is not specified, actual number will use as much (or as few) digits as it requires.


Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 01.04.2004