13.7.5. Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)

The sticky stream manipulators scientific and fixed control the output format of floating-point numbers. Stream manipulator scientific forces the output of a floating-point number to display in scientific format. Stream manipulator fixed forces a floating-point number to display a specific number of digits (as specified by member function precision or stream manipulator setprecision) to the right of the decimal point. Without using another manipulator, the floating-point-number value determines the output format.

Figure 13.18 demonstrates displaying floating-point numbers in fixed and scientific formats using stream manipulators scientific (line 18) and fixed (line 22). The exponent format in scientific notation might differ across different compilers.


 1   // Fig. 13.18: fig13_18.cpp
 2   // Floating-point values displayed in system default,
 3   // scientific and fixed formats.
 4   #include <iostream>
 5   using namespace std;
 6
 7   int main()
 8   {
 9      double x = 0.001234567;
10      double y = 1.946e9;
11
12      // display x and y in default format
13      cout << "Displayed in default format:" << endl
14         << x << ' ' << y << endl;
15
16      // display x and y in scientific format
17      cout << " Displayed in scientific format:" << endl
18         << scientific << x << ' ' << y << endl;
19
20      // display x and y in fixed format
21      cout << " Displayed in fixed format:" << endl
22         << fixed << x << ' ' << y << endl;
23   } // end main


Displayed in default format:
0.00123457      1.946e+009

Displayed in scientific format:
1.234567e-003   1.946000e+009

Displayed in fixed format:
0.001235        1946000000.000000


Fig. 13.18. Floating-point values displayed in default, scientific and fixed formats.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.195.34