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

10th Jul 2020, 11:58 AM
Lakshay Kumar
Lakshay Kumar - avatar
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.
10th Jul 2020, 12:35 PM
Vasile Eftodii
Vasile Eftodii - avatar
+ 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
10th Jul 2020, 12:21 PM
Vasile Eftodii
Vasile Eftodii - avatar
0
Instead of cout<<e<<endl Use printf("%.2f\n",e);
10th Jul 2020, 12:18 PM
Shobhith J B
Shobhith J B - avatar
0
Setprecision I tried but getting an error
10th Jul 2020, 12:22 PM
Lakshay Kumar
Lakshay Kumar - avatar
0
The printf can be used in c right not c++
10th Jul 2020, 12:22 PM
Lakshay Kumar
Lakshay Kumar - avatar
0
Will try thank you
10th Jul 2020, 1:02 PM
Lakshay Kumar
Lakshay Kumar - avatar