# Formatting Floating-Point Output

Problem

You 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.

Solution

Use the standard manipulators provided in and 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
#include
#include

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 << '
'; // 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 << '
'; // of the decimal.

cout << "pi = " << scientific // Scientific mode;
<< noshowpos // don't show plus sign anymore
<< pi * 1000 << '
';

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```

Discussion

Manipulators 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:

Normal (the default)

In this format, the number of digits displayed is fixed (with a default of six) and the decimal is displayed such that only a set number of digits are displayed at one time. So, by default, pi would be displayed as 3.14286, and pi times 100 would display 314.286.

Fixed

In this format, the number of digits displayed to the right of the decimal point is fixed, while the number of those displayed to the left is not. In this case, again with a default precision of six, pi would be displayed as 3.142857, and pi times 100 would be 314.285714. In both cases, the number of digits displayed to the right of the decimal point is six while the total number of digits can grow indefinitely.

Scientific

The value is shown as a single digit, followed by a decimal point, followed by a number of digits determined by the precision setting, followed by the letter "e" and the power of ten to raise the preceding value to. In this case, pi times 1,000 would display as 3.142857e+003.

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

Manipulator

Description

Sample output

Fixed

Show floating-point values with a fixed number of digits to the right of the decimal point.

With a default precision of six digits:

`pi = 3.142857`

scientific

Show floating-point values using scientific notation, which means a decimal number and an exponent multiplier.

pi * 1000, with a default precision of six digits:

`pi = 3.142857e+003`

setprecision

Control the number of digits displayed in the output. (See further explanation later.)

Pi in the default format, with a precision of 3:

`pi = 3.14`

In fixed format:

`pi = 3.143`

In scientific format:

`pi = 3.143e+000`

showposnoshowpos

Show a plus sign in front of positive numbers. This works for any kind of number, decimal or integer.

+3.14

showpointnoshowpoint

Show the decimal, even if there are only zeros after it. This works only for floating-point values, and not for integers.

The following line, with a precision of 2:

`cout << showpoint << 2.0`

will display like this:

`2.00`

showbasenoshowbase

Show the base for the number: decimal (none), octal (leading zero), or hexadecimal (leading 0x). See the next entry.

decocthex

Set the base for the numbers to be displayed to decimal, octal, or hexadecimal. The base is not shown by default; use showbase to display the base.

See previous entry.

Uppercasenouppercase

Display values in uppercase.

This sets the case for numeric output, such as 0X for hexadecimal numbers and E for numbers in scientific notation.

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 << '
';```

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 << '
';```

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 << '
';```

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

Recipe 10.3 Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2006
Pages: 241

Similar book on Amazon 