How to add two rounded doubles ?

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

closed account (48T7M4Gy)
http://www.cplusplus.com/reference/cmath/round/?kw=round

This is one good way and gives you lot's of options.
But this only includes the way to round to a int, I want to round off to 2 decimal places.
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/iomanip/setprecision/

1000 pardons
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>

float val = 37.777779;

float rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
float nearest = roundf(val * 100) / 100; /* Result: 37.78 */
float rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */
closed account (48T7M4Gy)
Well done. Here's a homegrown way along the lines of what u did, saves an include:

1
2
float x = 123.4567;
  std::cout << (float)((int)(x*100))/100;


Have to be careful though this is truncation, not rounding. :)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iomanip>

long long round_scale_100( double v ) { return std::llround( v * 100 ) ; }

std::string scaled_100_to_string( long long scaled_value )
{ return std::to_string( scaled_value / 100 ) + '.' + std::to_string( std::abs(scaled_value) % 100 ); }

int main()
{
    const double gas_charge = 169.726 ;
    const long long gas_charge_scaled = round_scale_100(gas_charge) ;

    const double adjustment = 48.906 ;
    const long long adjustment_scaled = round_scale_100(adjustment) ;

    const double maintenance_charge = 9.50 ;
    const long long maintenance_charge_scaled = round_scale_100(maintenance_charge) ;

    const long long total_cost_scaled = gas_charge_scaled + adjustment_scaled + maintenance_charge_scaled ;

    std::cout << "       Standard gas charge: " << std::setw(7) << std::right
              << scaled_100_to_string(gas_charge_scaled) << '\n'

              << "      Fuel cost adjustment: " << std::setw(7)
              << std::right << scaled_100_to_string(adjustment_scaled) << '\n'

              << "Monthly maintenance charge: " << std::setw(7)
              << std::right << scaled_100_to_string(maintenance_charge_scaled) << '\n'

              << "\n                Total cost: " << std::setw(7)
              << std::right << scaled_100_to_string(total_cost_scaled) << '\n' ;
}

       Standard gas charge:  169.73
      Fuel cost adjustment:   48.91
Monthly maintenance charge:    9.50

                Total cost:  228.14

http://coliru.stacked-crooked.com/a/1e05ccd7b111a77a
Topic archived. No new replies allowed.