<html lang="en"> <head> <title>Floating-Point Conversions - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" title="C-Style I/O Functions"> <link rel="prev" href="Integer-Conversions.html#Integer-Conversions" title="Integer Conversions"> <link rel="next" href="Other-Output-Conversions.html#Other-Output-Conversions" title="Other Output Conversions"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Floating-Point-Conversions"></a> <a name="Floating_002dPoint-Conversions"></a> <p> Next: <a rel="next" accesskey="n" href="Other-Output-Conversions.html#Other-Output-Conversions">Other Output Conversions</a>, Previous: <a rel="previous" accesskey="p" href="Integer-Conversions.html#Integer-Conversions">Integer Conversions</a>, Up: <a rel="up" accesskey="u" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions">C-Style I/O Functions</a> <hr> </div> <h4 class="subsection">14.2.9 Floating-Point Conversions</h4> <p>This section discusses the conversion specifications for floating-point 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>’ conversions. <p>The ‘<samp><span class="samp">%f</span></samp>’ conversion prints its argument in fixed-point notation, producing output of the form [<code>-</code>]<var>ddd</var><code>.</code><var>ddd</var><!-- /@w -->, where the number of digits following the decimal point is controlled by the precision you specify. <p>The ‘<samp><span class="samp">%e</span></samp>’ conversion prints its argument in exponential notation, producing output of the form [<code>-</code>]<var>d</var><code>.</code><var>ddd</var><code>e</code>[<code>+</code>|<code>-</code>]<var>dd</var><!-- /@w -->. Again, the number of digits following the decimal point is controlled by the precision. The exponent always contains at least two digits. The ‘<samp><span class="samp">%E</span></samp>’ conversion is similar but the exponent is marked with the letter ‘<samp><span class="samp">E</span></samp>’ instead of ‘<samp><span class="samp">e</span></samp>’. <p>The ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions print the argument in the style of ‘<samp><span class="samp">%e</span></samp>’ or ‘<samp><span class="samp">%E</span></samp>’ (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the ‘<samp><span class="samp">%f</span></samp>’ style. 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. <p>The following flags can be used to modify the behavior: <!-- Not @samp so we can have ` ' as an item. --> <dl> <dt>‘<samp><span class="samp">-</span></samp>’<dd>Left-justify the result in the field. Normally the result is right-justified. <br><dt>‘<samp><span class="samp">+</span></samp>’<dd>Always include a plus or minus sign in the result. <br><dt>‘<samp> </samp>’<dd>If the result doesn't start with a plus or minus sign, prefix it with a space instead. Since the ‘<samp><span class="samp">+</span></samp>’ flag ensures that the result includes a sign, this flag is ignored if you supply both of them. <br><dt>‘<samp><span class="samp">#</span></samp>’<dd>Specifies that the result should always include a decimal point, even if no digits follow it. For the ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions, this also forces trailing zeros after the decimal point to be left in place where they would otherwise be removed. <br><dt>‘<samp><span class="samp">0</span></samp>’<dd>Pad the field with zeros instead of spaces; the zeros are placed after any sign. This flag is ignored if the ‘<samp><span class="samp">-</span></samp>’ flag is also specified. </dl> <p>The precision specifies how many digits follow the decimal-point 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 these conversions, the default precision is <code>6</code>. If the precision is explicitly <code>0</code>, this suppresses the decimal point character entirely. For the ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ 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 <code>0</code> or not specified for ‘<samp><span class="samp">%g</span></samp>’ or ‘<samp><span class="samp">%G</span></samp>’, it is treated like a value of <code>1</code>. If the value being printed cannot be expressed precisely in the specified number of digits, the value is rounded to the nearest number that fits. </body></html>