Formatting Floating-Point OutputProblemYou need to present floating-point output in a well-defined format, either for the sake of precision (scientific versus fixed-point notation) or simply to line up decimal points vertically for easier reading. SolutionUse the standard manipulators provided in <iomanip> and <ios> to control the format of floating-point values that are written to the stream. There are too many combinations of ways to cover here, but Example 10-3 offers a few different ways to display the value of pi. Example 10-3. Formatting pi
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main( ) {
ios_base::fmtflags flags = // Save old flags
cout.flags( );
double pi = 3.14285714;
cout << "pi = " << setprecision(5) // Normal (default) mode; only
<< pi << '\n'; // show 5 digits, including both
// sides of decimal point.
cout << "pi = " << fixed // Fixed-point mode;
<< showpos // show a "+" for positive nums,
<< setprecision(3) // show 3 digits to the *right*
<< pi << '\n'; // of the decimal.
cout << "pi = " << scientific // Scientific mode;
<< noshowpos // don't show plus sign anymore
<< pi * 1000 << '\n';
cout.flags(flags); // Set the flags to the way they were
}
This will produce the following output: pi = 3.1429 pi = +3.143 pi = 3.143e+003 DiscussionManipulators that specifically manipulate floating-point output divide into two categories. There are those that set the format, which, for the purposes of this recipe, set the general appearance of floating-point and integer values, and there are those that fine-tune the display of each format. The formats are as follows:
Table 10-2 shows all manipulators that affect floating-point output (and sometimes numeric output in general). See Table 10-1 for general manipulators you can use together with the floating-point manipulators. Table 10-2. Floating-point and numeric manipulators
In all three formats, all manipulators have the same effects except setprecision. In the default mode, "precision" refers to the number of digits on both sides of the decimal point. For example, to display pi in the default format with a precision of 2, do this:
cout << "pi = " << setprecision(2)
<< pi << '\n';
Your output will look like this: pi = 3.1 By comparison, consider if you want to display pi in fixed-point format instead:
cout << "pi = " << fixed
<< setprecision(2)
<< pi << '\n';
Now the output will look like this: pi = 3.14 This is because, in fixed-point format, the precision refers to the number of digits to the right of the decimal point. If we multiply pi by 1,000 in the same format, the number of digits to the right of the decimal remains unchanged:
cout << "pi = " << fixed
<< setprecision(2)
<< pi * 1000 << '\n';
produces: pi = 3142.86 This is nice, because you can set your precision, set your field width with setw, right-justify your output with right (see Recipe 10.1 ), and your decimal points will all be lined up vertically. Since a manipulator is just a convenient way of setting a format flag on the stream, remember that the settings stick around until you undo them or until the stream is destroyed. Save the format flags (see Example 10-3) before you start making changes, and restore them when you are done |