Format field: precision

The precision specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be added, the number of decimal places, or the number of significant digits. Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value.

 

The type determines the interpretation of precision and the default when precision is omitted, as shown in table:

 

Type Meaning Default
c, C precision has no effect.  
d, i, u, o, x, X precision specifies the minimum number of digits to be added. 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 precision specifies the number of digits to be added after the decimal point. The last added digit is rounded. Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is added.
f 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 added.
g, G precision specifies the maximum number of significant digits added. Six significant digits are added, with any trailing zeros truncated.
s, S

precision specifies the maximum number of characters to be added. Characters in excess of precision are not added.

 

Note: With s, precision is the number of bytes. In Unicode mode, non-ASCII characters have several bytes. In QM 2.3.3 and later you can use flag # to specify that precision must be number of characters. Example: out "%#.5s" "ąčęėįšųūž"

Characters are added until a null character is encountered.
m precision specifies the number of bytes to be added. 0

 

Example:

str s.format("%.4s %.3f" "123456789" 1.12345)
out s ;;1234 1.123

 

If the precision specification is an asterisk (*), an int argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list. Example:

str s.format("%.*s %.*f" 4 "123456789" 3 1.12345)
out s ;;1234 1.123