0
Rounding off
I want my float output to be rounded off to 2 decimal places is it possible https://code.sololearn.com/c8gUP7UICfgL/?ref=app
6 Réponses
+ 2
I just added this line in your code and the output shows with no problem:
cout << setprecision(3) << f;
C++ is case-sensitive!
C++ is backward compatible with C so you may also use printf() if you like.
+ 1
For output you may use the setprecision(x) manipulator:
cout << setprecision(3) << value;
https://www.cprogramming.com/tutorial/iomanip.html
If you want to truncate the variable as in a bank operation than try this to truncate to two decimals:
value = round(value * 100.0) / 100.0;
https://stackoverflow.com/questions/14369673/round-double-to-3-points-decimal
0
Instead of cout<<e<<endl
Use printf("%.2f\n",e);
0
Setprecision I tried but getting an error
0
The printf can be used in c right not c++
0
Will try thank you