In a format specification, the third optional field is the precision specification. It consists of a period (.) followed by a non-negative decimal integer that, depending on the conversion type, specifies the number of string characters, the number of decimal places, or the number of significant digits to be output.
Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value. If an explicit precision of zero is specified and the format specifier for the field is one of the integer formats (i.e. d, i, o, u, x) and the value to be printed is 0, no characters are printed for that field (i.e., the field is empty).
The type determines either the interpretation of precision or the default precision when precision is omitted, as shown in the following table.
Type | Meaning | Default |
---|---|---|
d, i, u, o, x, X | The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. | Default precision is 1. |
e, E | The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. | Default precision is 6. If precision is 0 or the period (.) appears without a number following it, no decimal point is printed. |
f | The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. | Default precision is 6. If precision is 0, or if the period (.) appears without a number following it, no decimal point is printed. |
g, G | The precision specifies the maximum number of significant digits printed. | Six significant digits are printed, and any trailing zeros are truncated. |
c | The precision has no effect. | Character is printed. |
s, S | The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed. | Characters are printed until a null character is encountered. |