Node:Floating-Point Conversions, Next:Other Output Conversions, Previous:Integer Conversions, Up:Formatted Output
This section discusses the conversion specifications for floating-point
numbers: the %f
, %e
, %E
, %g
, and %G
conversions.
The %f
conversion prints its argument in fixed-point notation,
producing output of the form
[-
]ddd.
ddd,
where the number of digits following the decimal point is controlled
by the precision you specify.
The %e
conversion prints its argument in exponential notation,
producing output of the form
[-
]d.
ddde
[+
|-
]dd.
Again, the number of digits following the decimal point is controlled by
the precision. The exponent always contains at least two digits. The
%E
conversion is similar but the exponent is marked with the letter
E
instead of e
.
The %g
and %G
conversions print the argument in the style
of %e
or %E
(respectively) if the exponent would be less
than -4 or greater than or equal to the precision; otherwise they use
the %f
style. A precision of 0
, is taken as 1. is
Trailing zeros are removed from the fractional portion of the result and
a decimal-point character appears only if it is followed by a digit.
The %a
and %A
conversions are meant for representing
floating-point numbers exactly in textual form so that they can be
exchanged as texts between different programs and/or machines. The
numbers are represented is the form
[-
]0x
h.
hhhp
[+
|-
]dd.
At the left of the decimal-point character exactly one digit is print.
This character is only 0
if the number is denormalized.
Otherwise the value is unspecified; it is implementation dependent how many
bits are used. The number of hexadecimal digits on the right side of
the decimal-point character is equal to the precision. If the precision
is zero it is determined to be large enough to provide an exact
representation of the number (or it is large enough to distinguish two
adjacent values if the FLT_RADIX
is not a power of 2,
see Floating Point Parameters). For the %a
conversion
lower-case characters are used to represent the hexadecimal number and
the prefix and exponent sign are printed as 0x
and p
respectively. Otherwise upper-case characters are used and 0X
and P
are used for the representation of prefix and exponent
string. The exponent to the base of two is printed as a decimal number
using at least one digit but at most as many digits as necessary to
represent the value exactly.
If the value to be printed represents infinity or a NaN, the output is
[-
]inf
or nan
respectively if the conversion
specifier is %a
, %e
, %f
, or %g
and it is
[-
]INF
or NAN
respectively if the conversion is
%A
, %E
, or %G
.
The following flags can be used to modify the behavior:
-
+
+
flag ensures that the result includes
a sign, this flag is ignored if you supply both of them.
#
%g
and %G
conversions,
this also forces trailing zeros after the decimal point to be left
in place where they would otherwise be removed.
'
LC_NUMERIC
category;
see General Numeric. This flag is a GNU extension.
0
-
flag is also
specified.
The precision specifies how many digits follow the decimal-point
character for the %f
, %e
, and %E
conversions. For
these conversions, the default precision is 6
. If the precision
is explicitly 0
, this suppresses the decimal point character
entirely. For the %g
and %G
conversions, the precision
specifies how many significant digits to print. Significant digits are
the first digit before the decimal point, and all the digits after it.
If the precision is 0
or not specified for %g
or %G
,
it is treated like a value of 1
. If the value being printed
cannot be expressed accurately in the specified number of digits, the
value is rounded to the nearest number that fits.
Without a type modifier, the floating-point conversions use an argument
of type double
. (By the default argument promotions, any
float
arguments are automatically converted to double
.)
The following type modifier is supported:
L
L
specifies that the argument is a long
double
.
Here are some examples showing how numbers print using the various
floating-point conversions. All of the numbers were printed using
this template string:
"|%13.4a|%13.4f|%13.4e|%13.4g|\n"
Here is the output:
| 0x0.0000p+0| 0.0000| 0.0000e+00| 0| | 0x1.0000p-1| 0.5000| 5.0000e-01| 0.5| | 0x1.0000p+0| 1.0000| 1.0000e+00| 1| | -0x1.0000p+0| -1.0000| -1.0000e+00| -1| | 0x1.9000p+6| 100.0000| 1.0000e+02| 100| | 0x1.f400p+9| 1000.0000| 1.0000e+03| 1000| | 0x1.3880p+13| 10000.0000| 1.0000e+04| 1e+04| | 0x1.81c8p+13| 12345.0000| 1.2345e+04| 1.234e+04| | 0x1.86a0p+16| 100000.0000| 1.0000e+05| 1e+05| | 0x1.e240p+16| 123456.0000| 1.2346e+05| 1.235e+05|
Notice how the %g
conversion drops trailing zeros.