Outputting floating point numbers

The <ios> library has manipulators that alter how streams handle numbers. By default, the output stream will print floating-point numbers in a decimal format for numbers in the range 0.001 to 100000, and for numbers outside this range it will use a scientific format with a mantissa and exponent. This mixed format is the default behavior of the defaultfloat manipulator. If you always want to use scientific notation, then you should insert the scientific manipulator into the output stream.

If you want to display floating point numbers using just the decimal format (that is the whole number on the left side of a decimal point and the factional part on the right side), then modify the output stream with the fixed manipulator. The number of decimal places can be altered by calling the precision method:

    double d = 123456789.987654321; 
cout << d << "n";
cout << fixed;
cout << d << "n";
cout.precision(9);
cout << d << "n";
cout << scientific;
cout << d << "n";

The output from the preceding code is:

    1.23457e+08
123456789.987654
123456789.987654328
1.234567900e+08

The first line shows that scientific notation is used for large numbers. The second line shows the default behavior of fixed, which is to give the decimal number to 6 decimal places. This is changed in the code by calling the precision method to give 9 decimal places (the same effect can be achieved by inserting the setprecision manipulator in the <iomanip> library in the stream). Finally, the format is switched over to the scientific format with 9 decimal places to the mantissa from calling the precision method. The default is that the exponent is identified by the lowercase e. If you prefer, you can make this uppercase using the uppercase manipulator (and lowercase with nouppercase). Notice that the way that fractional parts are stored means that in fixed formats with 9 decimal places we see that the ninth digit is 8 rather than 1 as expected.

You can also specify whether a + symbol is shown for a positive number; the showpos manipulator will show the symbol, but the default noshowpos manipulator will not show the symbol. The showpoint manipulator will ensure that the decimal point is shown even if the floating-point number is a whole number. The default is noshowpoint, which means that, if there is no fractional part, no decimal point is displayed.

The setw manipulator (defined in the <iomanip> header) can be used with both integer and floating point numbers. In effect, this manipulator defines the minimum width of the space that the next (and only the next) item placed in the stream will occupy when printed on the console:

    double d = 12.345678; 
cout << fixed;
cout << setfill('#'),
cout << setw(15) << d << "n";

To illustrate the effect of the setw manipulator, this code calls setfill manipulator, which indicates that instead of spaces a hash symbol (#) should be printed. The rest of the code says that the number should be printed using the fixed format (to 6 decimal places by default) in a space 15 characters wide. The result is:

    ######12.345678

If the number is negative (or showpos is used), then by default the sign will be with the number; if the internal manipulator (defined in <ios>) is used, then the sign will be left-justified in the space set for the number:

    double d = 12.345678; 
cout << fixed;
cout << showpos << internal;
cout << setfill('#'),
cout << setw(15) << d << "n";

The result of the preceding code is as follows:

    +#####12.345678

Notice that the + sign to the right of the spaces is indicated by the pound symbol.

The setw manipulator is typically used to allow you to output tables of data in formatted columns:

    vector<pair<string, double>> table 
{ { "one",0 },{ "two",0 },{ "three",0 },{ "four",0 } };

double d = 0.1;
for (pair<string,double>& p : table)
{
p.second = d / 17.0;
d += 0.1;
}

cout << fixed << setprecision(6);

for (pair<string, double> p : table)
{
cout << setw(6) << p.first << setw(10) << p.second << "n";
}

This fills a vector of pairs with a string and a number. The vector is initialized with the string values and a zero, then the floating-point number is altered in the for loop (the actual calculation is irrelevant here; the point is to create some numbers with multiple decimal places). The data is printed out in two columns with the numbers printed with 6 decimal places. This means that, including the leading zero and decimal point, each number will take up 8 spaces. The text column is specified as being 6 characters wide and the number column is specified as 10 characters wide. By default, when you specify a column width, the output will be right justified, meaning that each number is preceded by two spaces and the text is padded according to the length of the string. The output looks like this:

    one  0.005882
two 0.011765
three 0.017647
four 0.023529

If you want the items in a column to be left justified, then you can use the left manipulator. This will affect all columns until the right manipulator is used to change the justification to right:

    cout << fixed << setprecision(6) << left;

The output from this will be:

    one   0.005882
two 0.011765
three 0.017647
four 0.023529

If you want different justification for the two columns, then you need to set the justification before printing a value. For example, to left justify the text and right justify the numbers, use this:

    for (pair<string, double> p : table) 
{
cout << setw(6) << left << p.first
<< setw(10) << right << p.second << "n";
}

The result of the preceding code is as follows:

    one     0.005882
two 0.011765
three 0.017647
four 0.023529
..................Content has been hidden....................

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