Originally Posted by
Salem
> i want it to return me only 1.66 or 1.67 for example.
Is that what you want to print, in which case it would be something like
Code:
double a = 5.0 / 3.0;
cout << setprecision(3) << a << endl;
If you really want to store the rounded results, then you need to understand that floats are approximations. So even if you round the result to 1.67, this may in fact be represented as 1.66999999 or 1.67000001 as being the nearest representable value.
For printing, using only setprecision is not enough:
Code:
double a = 50.0 / 3.0;
cout << setprecision(3);
cout << a << endl;
prints 16.7.
You'll need to use the "fixed" manipulator also, and specify the decimal places you want after the decimal point:
Code:
double a = 50.0 / 3.0;
cout << fixed << setprecision(2);
cout << a << endl;
(This prints 16.67)