C++ rounding off to the nearest....

Hey everyone, i've just started studying c++ and i'm doing / STUCK ON this task which requires me to round off a number that will be inputted (monetary value) to the nearest 5 cents, using 'if' statements and basic data types.

For example if 125.58 was entered it would round to 125.60, because the numbers in front of the decimal point aren't touched I don't know how to do it lol

Would I require to subtract the real value of the input from the original input then round it off?

Would this be right for a rounding off code for just basic numbers to 5?

int main ()
{
int price;

if (price = 1,2,3,4)
cout << "5" <<endl;
(etc...)

}

Thanks, help would be much appreciated!

Read in both the dollars and cents as individual integers. Then all you need to do is round the
integer.

(your if() check above has two problems. first, = is assignment, == is comparison, and second,
although it is legal syntax, "1,2,3,4" does not do what you want.)

I'm surprise they don't have a rounding function yet...
Rounding Algorithms
http://www.cplusplus.com/forum/articles/3638/

Calculations with money are often done in fixed point to avoid round-off errors. Much like jsmith suggested, except you multiply the number by 100 (or 10000). You can extract the cents with a simple remainder function (%), and the dollars with a simple integer division (/).

Hope this helps.
Thanks, the replies were useful, but the bill/cents are both set as "int" data types and I need to use if statements to round off the numbers, so I can't use the 'floor' code.

Still not sure on how to round off the integer :( I have an idea on how to do it, just don't know how the code would look like. If its 1 or 2 then it'll round to 0, 3 4 5 6 would round to 5, and 7, 8, 9 would round to 0 and the integer in front of it would +1, don't know how to put it into code form
Last edited on
I've found a way to round to the nearest 5 cents, but 'm still not sure on how to separate the cents value in the code from the original dollar.

1
2
3
4
5
6
7
8
    int num;
    cout<< "Enter a number: "<<endl;
    cin>> num;
    num=((num+2)/5)*5;
    if (num%5<3)
       cout << num - (num%5)<<endl;
    else
        cout << num + (5-num%5)<<endl;
I've converted the floating point price to integer then done the rounding off but on the output when I want it to go back to float/decimal I try to divide by 100 but it won't work
1
2
3
4
5
6
7
8
    cout << "Enter the bill: " << flush;
    cin >> bill; 
    bill=bill*100;
    bill=((static_cast<int>(bill)+2)/5)*5;
    if (static_cast<int>(bill)%5<3)
       cout << static_cast<int>(bill) - (static_cast<int>(bill)%5) / 100<<endl;
    else
        cout << static_cast<int>(bill) + (5-static_cast<int>(bill)%5) / 100 <<endl;
OK! i've done it, managed to round off the prices to the nearest 5 cents :)

It also tells me to produce an output that says: "Originalprice" was rounded to "roundedprice" in the output statement but i'm not sure how to obtain the original price on it's own and put it into the output without changing the rest of the code?

What I mean is... the original "bill" input e.g 122.34 then gets rounded off to 122.35, and the output should be "122.34 was rounded to 122.35" .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    bill=bill*100; // convert the bill into cents to round it off
    bill=((static_cast<int>(bill)+2)/5)*5;
    
    if (static_cast<int>(bill)%5<3)
    {
    bill = (static_cast<int>(bill) - (static_cast<int>(bill)%5));
    bill = bill/100;
    cout << setprecision(2) << fixed << bill << endl;
    }
    else
    {
    bill = (static_cast<int>(bill) + (5-static_cast<int>(bill)%5) / 100); 
    bill = bill/100;
    cout <<setprecision(2) << fixed << bill << endl;
    }


Also how would I split float BILL to dollars / cents (integer type) ?

I have float bill,
dollars = static_Cast<int>(bill)
cents = ?
Last edited on
I've gotten the integer for dollars / cents (although this one says "Warning converting from int to float"

1
2
3
billDollar = static_cast<int>(bill);
   billCents  = (bill * 100) - (static_cast<int>(bill)*100);
   cout << setprecision(2) << fixed << billCents << endl;



EDIT EDIT:

reworked the code so I don't get the warning though the output remains the same...

1
2
3
4
   billDollar = static_cast<int>(bill);
   bill = bill * 100;
   billCents  = (static_cast<int>(bill) - (billDollar*100));
   cout << setprecision(2) << fixed << billCents << endl;


but sometimes with the output, it will round off the bill to say... $500.20 but the billcents will only show it as "19"

Anyone know the problem?
Last edited on
Probably floating point precision errors:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Argh, thanks for the link firedraco, pity this is only my third week of C++ and I don't understand anything from that page =(, it will probably be of help later as I learn more.
Basically, just know that stuff like this can be bad:

1
2
double ten = 10.0;
asset(ten == 10.0-1.0+1.0); //this might cause an error 
...which is why you were recommended to use integers and stay away from floats.
+1

That many casts is pretty much a clear indication that you're using the wrong data type.
Topic archived. No new replies allowed.