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
3.133.159.223