![]() |
VOOZH | about |
In C++, floating-point values are printed using a default format, but it can be customized for precision, style, and rounding using stream manipulators. These tools help control how numeric output appears.
setprecision(n) function lets you control the number of significant digits displayed when printing floating-point numbers. By default, the precision is set to 6 digits, but this can be increased or decreased as needed.
123.5 123.4568
Explanation: setprecision(4) and setprecision(7) allow 4 and 7 significant digits to be printed respectively.
By default, trailing zeros after the decimal point are omitted to keep the output compact. The showpoint() function forces the display of trailing zeros, while noshowpoint() reverts back to the default behavior.
50 50.0000 50
Explanation: showpoint(): forces the display of trailing zeros after the decimal point, so the output becomes 50.0000, whereas noshowpoint(): hides the trailing zeros and restores the default compact format, so the output becomes 50.
Note: In the showpoint() output, only 4 zeros appear after the decimal because the default precision is 6 significant digits, so 50 already uses two digits and the remaining four are shown as zeros.
By default, only negative numbers are shown with a sign (-). The showpos() function explicitly displays the + sign for positive numbers, while noshowpos() disables it.
10 -10 +10 -10 10 -10
Explanation: showpos forces the + sign to appear before positive numbers (+10), while noshowpos removes it and restores the normal output.
When using scientific notation, the e in the output is lowercase by default. The uppercase() function converts it to uppercase (E), while nouppercase() reverts this change.
1.234568e+04 1.234568E+04 1.234568e+04
Explanation: scientific prints the number in scientific notation, uppercase changes 'e' to 'E' , and nouppercase changes it back to lowercase 'e'.