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.
- Manipulators like setprecision, fixed, and scientific change the display format of numbers.
- They are useful for producing clear, readable, and well-formatted numeric output.
Floating-Point Stream Manipulators in C++:
1. Changing Precision with setprecision(n):
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.
#include <bits/stdc++.h>
using namespace std;
int main() {
double x = 123.456789;
cout << setprecision(4) << x << endl;
cout << setprecision(7) << x << endl;
return 0;
}
Output
123.5 123.4568
Explanation: setprecision(4) and setprecision(7) allow 4 and 7 significant digits to be printed respectively.
2. Showing or Hiding Trailing Zeros with showpoint():
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.
#include <bits/stdc++.h>
using namespace std;
int main() {
double x = 50.0;
cout << x << endl;
cout << showpoint << x << endl;
cout << noshowpoint << x << endl;
return 0;
}
Output
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.
3. Displaying Positive Signs with showpos():
By default, only negative numbers are shown with a sign (-). The showpos() function explicitly displays the + sign for positive numbers, while noshowpos() disables it.
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 10;
int b = -10;
cout << a << " " << b << endl;
cout << showpos << a << " " << b << endl;
cout << noshowpos << a << " " << b << endl;
return 0;
}
Output
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.
4. Changing Exponential Notation Case with uppercase():
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.
#include <bits/stdc++.h>
using namespace std;
int main() {
double x = 12345.678;
cout << scientific << x << endl;
cout << uppercase << x << endl;
cout << nouppercase << x << endl;
return 0;
}
Output
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'.