Hello, I'm new to the forums and would really appreciate your help. I seem to be missing something with the setprecision() function. I want to round a floating point with 3 decimal places up to 2 decimal places.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double x = 5.555;
cout << "x is " << fixed << setprecision(2) << x << endl;
return 0;
}
If I compile this (using g++), the output is
x is 5.55
However, if I change x to 5.556 it rounds up correctly to 5.56. What am I doing wrong?
Question
danb_03
Hello, I'm new to the forums and would really appreciate your help. I seem to be missing something with the setprecision() function. I want to round a floating point with 3 decimal places up to 2 decimal places.
If I compile this (using g++), the output is
However, if I change x to 5.556 it rounds up correctly to 5.56. What am I doing wrong?
Link to comment
Share on other sites
4 answers to this question
Recommended Posts