Formatting for Floating-Point Numbers

The formatting capabilities in Fig. 4.10 are discussed here briefly and explained in depth in Chapter 13, Stream Input/Output: A Deeper Look. The call to setprecision in line 75 (with an argument of 2) indicates that double variable average should be printed with two digits of precision to the right of the decimal point (e.g., 92.37). This call is referred to as a parameterized stream manipulator (because of the 2 in parentheses). Programs that use these calls must contain the preprocessing directive (line 5)

#include <iomanip>

The manipulator endl is a nonparameterized stream manipulator (because it isn’t followed by a value or expression in parentheses) and does not require the <iomanip> header. If the precision is not specified, floating-point values are normally output with six digits of precision (i.e., the default precision on most of today’s systems), although we’ll see an exception to this in a moment.

The stream manipulator fixed (line 75) indicates that floating-point values should be output in so-called fixed-point format, as opposed to scientific notation. Scientific notation is a way of displaying a number as a floating-point number between the values of 1.0 and 10.0, multiplied by a power of 10. For instance, the value 3,100.0 would be displayed in scientific notation as 3.1 × 103. Scientific notation is useful when displaying values that are very large or very small. Formatting using scientific notation is discussed further in Chapter 13. Fixed-point formatting, on the other hand, is used to force a floating-point number to display a specific number of digits. Specifying fixed-point formatting also forces the decimal point and trailing zeros to print, even if the value is a whole number amount, such as 88.00. Without the fixed-point formatting option, such a value prints in C++ as 88 without the trailing zeros and without the decimal point. When the stream manipulators fixed and setprecision are used in a program, the printed value is rounded to the number of decimal positions indicated by the value passed to setprecision (e.g., the value 2 in line 75), although the value in memory remains unaltered. For example, the values 87.946 and 67.543 are output as 87.95 and 67.54, respectively. It’s also possible to force a decimal point to appear by using stream manipulator showpoint. If showpoint is specified without fixed, then trailing zeros will not print. Like endl, stream manipulators fixed and showpoint do not use parameters, nor do they require the <iomanip> header. Both can be found in header <iostream>.

Lines 75 and 76 of Fig. 4.10 output the class average rounded to the nearest hundredth and with exactly two digits to the right of the decimal point. The parameterized stream manipulator (line 75) indicates that variable average’s value should be displayed with two digits of precision to the right of the decimal point—as indicated by setprecision(2). The three grades entered during the execution of the program in Fig. 4.11 total 257, which yields the average 85.666... and prints with rounding as 85.67.

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

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