I am writing a program to calculate the bills by inputting values. After a bunch of calculations, I will output the calculated Standard gas charge and Fuel cost adjustment and then sum them up to output the cost.
I've used setprecision(2).
But the problem is the actual value of the below Standard gas charge is 169.726 and Fuel cost adjustment is 48.906. When I add those value up it will display 228.13 but I want to have 228.14. How can I directly add the rounded off values instead of their actual values?
This is what I want to achieve.
Standard gas charge: 169.73
Fuel cost adjustment: 48.91
Monthly maintenance charge: 9.50
Cost: 228.14
I have used it already but it could only fulfil the output purpose and can not really change the actual value into two decimals and do the calculations. But anyway I googled it and find that
I could use this method. And I solved it now. Thank you.
#include <math.h>
Floating point values and computations are approximate. One way is to multiply the floating point value by 100, round to the nearest integer, and while printing, place a decimal point before the last two digits.