3
<title>Floating-Point Conversions - Untitled</title>
4
<meta http-equiv="Content-Type" content="text/html">
5
<meta name="description" content="Untitled">
6
<meta name="generator" content="makeinfo 4.11">
7
<link title="Top" rel="start" href="index.html#Top">
8
<link rel="up" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" title="C-Style I/O Functions">
9
<link rel="prev" href="Integer-Conversions.html#Integer-Conversions" title="Integer Conversions">
10
<link rel="next" href="Other-Output-Conversions.html#Other-Output-Conversions" title="Other Output Conversions">
11
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12
<meta http-equiv="Content-Style-Type" content="text/css">
13
<style type="text/css"><!--
14
pre.display { font-family:inherit }
15
pre.format { font-family:inherit }
16
pre.smalldisplay { font-family:inherit; font-size:smaller }
17
pre.smallformat { font-family:inherit; font-size:smaller }
18
pre.smallexample { font-size:smaller }
19
pre.smalllisp { font-size:smaller }
20
span.sc { font-variant:small-caps }
21
span.roman { font-family:serif; font-weight:normal; }
22
span.sansserif { font-family:sans-serif; font-weight:normal; }
28
<a name="Floating-Point-Conversions"></a>
29
<a name="Floating_002dPoint-Conversions"></a>
30
Next: <a rel="next" accesskey="n" href="Other-Output-Conversions.html#Other-Output-Conversions">Other Output Conversions</a>,
31
Previous: <a rel="previous" accesskey="p" href="Integer-Conversions.html#Integer-Conversions">Integer Conversions</a>,
32
Up: <a rel="up" accesskey="u" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions">C-Style I/O Functions</a>
36
<h4 class="subsection">14.2.9 Floating-Point Conversions</h4>
38
<p>This section discusses the conversion specifications for floating-point
39
numbers: the ‘<samp><span class="samp">%f</span></samp>’, ‘<samp><span class="samp">%e</span></samp>’, ‘<samp><span class="samp">%E</span></samp>’, ‘<samp><span class="samp">%g</span></samp>’, and ‘<samp><span class="samp">%G</span></samp>’
42
<p>The ‘<samp><span class="samp">%f</span></samp>’ conversion prints its argument in fixed-point notation,
43
producing output of the form
44
[<code>-</code>]<var>ddd</var><code>.</code><var>ddd</var><!-- /@w -->,
45
where the number of digits following the decimal point is controlled
46
by the precision you specify.
48
<p>The ‘<samp><span class="samp">%e</span></samp>’ conversion prints its argument in exponential notation,
49
producing output of the form
50
[<code>-</code>]<var>d</var><code>.</code><var>ddd</var><code>e</code>[<code>+</code>|<code>-</code>]<var>dd</var><!-- /@w -->.
51
Again, the number of digits following the decimal point is controlled by
52
the precision. The exponent always contains at least two digits. The
53
‘<samp><span class="samp">%E</span></samp>’ conversion is similar but the exponent is marked with the letter
54
‘<samp><span class="samp">E</span></samp>’ instead of ‘<samp><span class="samp">e</span></samp>’.
56
<p>The ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions print the argument in the style
57
of ‘<samp><span class="samp">%e</span></samp>’ or ‘<samp><span class="samp">%E</span></samp>’ (respectively) if the exponent would be less
58
than -4 or greater than or equal to the precision; otherwise they use the
59
‘<samp><span class="samp">%f</span></samp>’ style. Trailing zeros are removed from the fractional portion
60
of the result and a decimal-point character appears only if it is
63
<p>The following flags can be used to modify the behavior:
65
<!-- Not @samp so we can have ` ' as an item. -->
67
<dt>‘<samp><span class="samp">-</span></samp>’<dd>Left-justify the result in the field. Normally the result is
70
<br><dt>‘<samp><span class="samp">+</span></samp>’<dd>Always include a plus or minus sign in the result.
72
<br><dt>‘<samp> </samp>’<dd>If the result doesn't start with a plus or minus sign, prefix it with a
73
space instead. Since the ‘<samp><span class="samp">+</span></samp>’ flag ensures that the result includes
74
a sign, this flag is ignored if you supply both of them.
76
<br><dt>‘<samp><span class="samp">#</span></samp>’<dd>Specifies that the result should always include a decimal point, even
77
if no digits follow it. For the ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions,
78
this also forces trailing zeros after the decimal point to be left
79
in place where they would otherwise be removed.
81
<br><dt>‘<samp><span class="samp">0</span></samp>’<dd>Pad the field with zeros instead of spaces; the zeros are placed
82
after any sign. This flag is ignored if the ‘<samp><span class="samp">-</span></samp>’ flag is also
86
<p>The precision specifies how many digits follow the decimal-point
87
character for the ‘<samp><span class="samp">%f</span></samp>’, ‘<samp><span class="samp">%e</span></samp>’, and ‘<samp><span class="samp">%E</span></samp>’ conversions. For
88
these conversions, the default precision is <code>6</code>. If the precision
89
is explicitly <code>0</code>, this suppresses the decimal point character
90
entirely. For the ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions, the precision
91
specifies how many significant digits to print. Significant digits are
92
the first digit before the decimal point, and all the digits after it.
93
If the precision is <code>0</code> or not specified for ‘<samp><span class="samp">%g</span></samp>’ or
94
‘<samp><span class="samp">%G</span></samp>’, it is treated like a value of <code>1</code>. If the value being
95
printed cannot be expressed precisely in the specified number of digits,
96
the value is rounded to the nearest number that fits.