Field Width and Decimal Precision

Total Page:16

File Type:pdf, Size:1020Kb

Field Width and Decimal Precision

Field Width and Decimal Precision (sec 3-5)

By default, C++ prints real numbers with zero to six digits after the decimal point. This is not necessarily what we want, e.g. dollars and cents.

I'll just give you the code to specify the decimal precision, the number of digits after the decimal point, without any explanation. Add these two lines to a program in which you want to print real values with two decimal places:

cout.setf(ios::fixed,ios::floatfield); cout.precision(2); The first line tells the computer that we want the line cout.precision() to specify the number of decimal places. After this is done, the line cout.precision(2) specifies that we want floating point numbers to print with two decimal places. Notes:  If you omit the call to cout.setf(), the call to cout.precision() will specify the number of significant digits.  A call to cout.precision() and cout.setf() stays in effect for the rest of the program or until the next call that overrides it. This is called persistent.

double cost = 43.67645;

cout << cost;

The output from this (in at least one compiler) will be 43.6765, with a total of six positions. We can print a different number of digits after the decimal point:

cout.setf(ios::fixed,ios::floatfield); cout.precision(2); cout << cost << endl; cout.precision(6); cout << cost << endl;

The output is the following:

43.68 43.676450

Note that the values are rounded off: 43.6735 becomes 43.67

Setting the Field Width: width()

To specify the exact number of character positions a value should occupy when it is printed, use a call to cout.width(). EXAMPLE 3-8a: In Program 2 in the text, they wanted to print the values of gpa and result neatly aligned in columns; here are two of those sets of values:

1.5 154.5 2 12.6

In Chapter 2, we created columns by using tabs, but a nicer way is to use calls to cout.width() inside the loop:

cout.width(10); cout << gpa; cout.width(10); cout << result;

Output: The values would print right-aligned in columns 10 spaces wide:

1.5 154.5 2 12.6

WARNING,Warning,Warning A call to cout.width() applies only to the very next value to be printed. It doesn't even apply to two values printed in the same cout. This is different from calls to cout.precision() and cout.setf().

Left and Right Alignment

Once you have specified a field width using cout.width(), you can also specify whether you want the value left or right aligned within its field. Left aligned means starting in the far left position in the field; right aligned means ending in the far right position. Right alignment is the default for numbers.

Default is right alignment. To set alignment, use a call to cout.setf(), specifying left or right alignment: cout.setf(ios::left); // aligns left cout.setf(ios::right); // aligns right

The value set in this way remains in effect for the rest of the program or until the next call that overrides it. What do we call that? Persistent.

You must know above for the final. SEE try_i_o.cpp for a better way. Not in the text. Must know for my exam.

There are additional I/O stream manipulators defined in iomanip. They ease formatting.

It is a common practice for C++ programmers to #include along with .

Need fixed for setprecision to be digits after decimal point, rather than just digits. setw() is not persistent. It applies only to the next item. cout << left inserts a left justification flag in the output stream.

You can embed the modifier in the output stream. You can put multiple values in one cout statement: cout << fixed << setprecision(2) << setw(10) << gpa << setw(10) << result;

If-else SEE my handwritten notes.

Recommended publications