Syntax of Format Descriptors
The format you specify applies to a single floating point value, but it is replicated for all appropriate print values. The syntax used to specify the format comes from the C/C++ programming languages. For a more complete treatment of this subject, refer to any good text that describes the printf family of functions of the c or C++ programming language. The following is merely a representative example of what may be specified.
A format specification always begins with a percent sign; i.e., %. The only other required element is the conversion operation, which must be the last element in the format.
The remainder of the conversion specification must appear in the following order:
Zero or more optional flag characters from this list:
- (minus sign) Left-justify within the field width. If this item is absent, the data will be right-justified.
0 (zero) Use zero instead of blank to pad the value to fill the field width.
+ (plus sign) Always display a sign, either plus or minus; i.e., + or - (minus) .
space Display a blank rather than a plus for positive values; display a minus sign for negative values.
# (pound sign) specifies use of a variant that is not discussed here.
Field width in characters specified as an integer value without leading zeros (which could be misinterpreted as the "zero flag" described above). This also is optional. An alternative to an integer value is to use an asterisk, not discussed here. The largest expected value, including sign, decimal, exponent, or other formatting, should fit within the width specified here.
An optional precision specifier expressed as a decimal point (i.e., a period) followed by an integer value that specifies the number of decimal places to display. Using a negative precision specifier results in unpredictable behavior. Alternatively, an asterisk may be used after the period, but is not discussed here.
An optional long size specifier, expressed as a lower case L. This is not discussed further here.
A conversion operation, which is required. Since it applies to floating point values, appropriate values for the conversion operation include:
f Standard notation of the form [-]dd.ddd where the precision specifier designates the number of digits to the right of the decimal point. This example requires a field with of 7 characters (2 integer digits, 3 decimal digits, sign and decimal point). A typical field width might be 12 digits; i.e., % 12.3f uses blanks for padding, field width of 12 characters with 3 decimal digits.
E or e Modified scientific notation with one digit to the left of the decimal point. The precision specifier designates the number of digits to the right of the decimal point. This is followed by an exponent of the form: E+ee, where "ee" is a two digit integer representing the power assigned to the value. For example, to express a value with 5 decimal digits, one could use % 12.5e. Note the field width of 12 for 1 integer digit, 5 decimal digits, four spaces for "E+ee" or "E-ee" as appropriate, one space for the decimal point, and one for the sign, for a total of 12.
G or g Basically a blend of the "f" and "e" conversion operations. If the value fits within the field width, the f the precision specifier is used. If the value is too large, the e operation is applied. The precision specifier designates the number of digits to the right of the decimal point. Note that, since the e operation uses 4 characters of the field width to display the exponent, for a fixed field width, it displays fewer digits than does the f operation. In fact, the e operation requires a minimum field width of 7 to display a single numeric digit. For example, % G7.3 displays the value "-12.3456" as "-12.345" truncated to 7 characters, while "-123.456" requires an 8 character width to display 3 decimal digits, so it appears as "-1.E+02".