Formatting Console Output in C++

You can use the stream manipulators to display formatted output on the console.

Often it is desirable to display numbers in a certain format. For example, the following code computes interest, given the amount and the annual interest rate.

double amount = 12618.98;

double interestRate = 0.0013;

double interest = amount * interestRate;

cout << “Interest is ” << interest << endl;

Because the interest amount is currency, it is desirable to display only two digits after the decimal point. To do this, you may write the code as follows:

double amount = 12618.98;

double interestRate = 0.0013;

double interest = amount * interestRate;

cout << “Interest is “

<< static_cast<char>(interest * 100) / 100.0 << endl;

However, the format is still not correct. There should be two digits after the decimal point (i.e., 16.40 rather than 16.4). You can fix it by using formatting functions, like this:

double amount = 12618.98;

double interestRate = 0.0013;

double interest = amount * interestRate;

cout << “Interest is ” << fixed << setprecision(2)

<< interest << endl;

You already know how to display console output using the cout object. C++ provides addi­tional functions for formatting how a value is displayed. These functions are called stream manipulators and are included in the iomanip header file. Table 4.8 summarizes several use­ful stream manipulators.

1. setprecision(n) Manipulator

You can specify the total number of digits displayed for a floating-point number using the setprecision(n) manipulator, where n is the number of significant digits (i.e., the total number of digits that appear before and after the decimal point). If a number to be displayed has more digits than the specified precision, it will be rounded. For example, the code

double number = 12.34567;

cout << setprecision(3)  << number     <<     ” “

<< setprecision(4)   << number     <<    ” “

<< setprecision(5)   << number     <<    ” “

<< setprecision(6)   << number     <<    endl;

displays

where the square box (B) denotes a blank space.

The value of number is displayed using precision 3, 4, 5, and 6, respectively. Using preci­sion 3, 12.34567 is rounded to 12.3. Using precision 4, 12.34567 is rounded to 12.35. Using precision 5, 12.34567 is rounded to 12.346. Using precision 6, 12.34567 is rounded to 12.3457.

The setprecision manipulator remains in effect until the precision is changed. So,

double number = 12.34567;

cout << setprecision(3) << number << ” “;

cout << 9.34567 << ” ” << 121.3457 << ” ” << 0.2367 << endl; displays

The precision is set to 3 for the first value, and it remains effective for the next two values, because it has not been changed.

If the width is not sufficient for an integer, the setprecision manipulator is ignored. For example,

cout << setprecision(3) << 23456 << endl;

displays

23456

2. fixed Manipulator

Sometimes, the computer automatically displays a large floating-point number in scientific notation. On the Windows machine, for example, the statement

cout << 232123434.357;

displays

2.32123e+08

You can use the fixed manipulator to force the number to be displayed in nonscientific nota­tion with a fixed number of digits after the decimal point. For example,

cout << fixed << 232123434.357;

displays

232123434.357000

By default, the fixed number of digits after the decimal point is 6. You can change it using the fixed manipulator along with the setprecision manipulator. When it is used after the fixed manipulator, the setprecision manipulator specifies the number of digits after the decimal point. For example,

double monthlyPayment = 345.4567;

double totalPayment = 78676.887234;

cout << fixed << setprecision(2)

<< monthlyPayment << endl

<< totalPayment << endl;

displays

345.46

78676.89

3. showpoint Manipulator

By default, floating-point numbers that do not have a fractional part are not displayed with a decimal point. You can use the fixed manipulator to force the floating-point numbers to be displayed with a decimal point and a fixed number of digits after the decimal point. Alternatively, you can use the showpoint manipulator together with the setprecision manipulator.

For example,

cout << setprecision(6);

cout << 1.23 << endl;

cout << showpoint << 1.23 << endl;

cout << showpoint << 123.0 << endl;

displays

1.23

1.23000

123.000

The setprecision(6) function sets the precision to 6. So, the first number 1.23 is dis­played as 1.23. Because the showpoint manipulator forces the floating-point number to be
displayed with a decimal point and trailing zeros if necessary to fill in the positions, the second number 1.23 is displayed as 1.23000 with trailing zeros, and the third number 123.0 is displayed as 123.000 with a decimal point and trailing zeros.

4. setw(width) Manipulator

By default, cout uses just the number of the positions needed for an output. You can use setw(width) to specify the minimum number of columns for an output. For example,

cout << setw(8) << “C++” << setw(6) << 101 << endl;

cout << setw(8) << “Java” << setw(6) << 101 << endl;

cout << setw(8) << “HTML” << setw(6) << 101 << endl;

displays

The output is right-justified within the specified columns. In line 1, setw(8) specifies that “C++” is displayed in eight columns. So, there are five spaces before C++. setw(6) specifies that 101 is displayed in six columns. So, there are three spaces before 101.

Notice that the setw manipulator affects only the next output. For example,

cout << setw(8) << “C++” << 101 << endl;

displays

The setw(8) manipulator affects only the next output “C++”, not 101.

Note that the argument n for setw(n) and setprecision(n) can be an integer variable, expression, or constant.

If an item requires more spaces than the specified width, the width is automatically increased. For example, the following code

cout << setw(8) << “Programming” << “#” << setw(2) << 101;

displays

Programming#101

The specified width for Programming is 8, which is smaller than its actual size 11. The width is automatically increased to 11. The specified width for 101 is 2, which is smaller than its actual size 3. The width is automatically increased to 3.

5. left and right Manipulators

Note that the setw manipulator uses right justification by the default. You can use the left manipulator to left-justify the output and use the right manipulator to right-justify the out­put. For example,

cout << right;

cout << setw(8) << 1.23 << endl;

cout << setw(8) << 351.34 << endl;

displays

cout << left;

cout << setw(8) << 1.23;

cout << setw(8) << 351.34 << endl;

displays

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *