1. ## Rounding Numbers accurately

I am trying to find a simple solution to rounding numbers in C++. My dilemma is that I am doing a tax calculation that is .0875. When I use it in my calculation it rounds up perfect however when I add it to my end totals it loses .5 so therefore when I add all my totals up with the two percentage calculations I have..I lose 1 cent. How do I get this to not lose the .5.

2. Simple answer, don't use floating point numbers when you need absolute precision, instead use 2 integers, one for the dollar amount and one for the cents.

3. If I'm understanding you correctly, what you want is something like this:
add .5 to your total, then use floor() (in <cmath>, I think) to round it off. If the original number should round up, you'll have gone up past the rounded up number, and floor() will give the correct round up number. If you need to round down, the max you'll reach is .9, and will then round down.
Code:
```double x = 1.4;  // start with a value, should round down to 1
x += 0.5;           // add .5, x now equals 1.9
x = floor(x);        // x rounded up to 1

double y = 1.6;  // start with a value, should round up to 2
y += 0.5;           // add .5, y now equals 2.1
y = floor(y);       // y rounded up to 2```
You could do the same thing by subtracting .5 and using ceil() instead.

4. I think both of the replys to this post is going in the direction of what I want in a result. Here is my problem. I am still new to C++ programming so somethings are hard for me to do. I have wrote this out and desk checked, ran in my program. What I got as a result was correct however it effected some of my other totals. In my program if a student wants to take an even number of classes the total is correct however when they want to take an odd number of classes the total chops of 1 penny. This below works on the one penny however it also adds one penny to the even classes.

I need it to not round up on the even number of classes so should I put in a statement that compares the remainders and if the remainders are above .5 I round up and below .5 I round down. Can I do that by putting a floor into this below??

//Calculates taxes on tuition.
float calcTAXES(float FlatTuition)
{
float TaxTotal = 0.0;
TaxTotal = FlatTuition * TAXES;
TaxTotal = TaxTotal * 100 + .05;
TaxTotal = ceil(TaxTotal);
TaxTotal = TaxTotal / 100;
return TaxTotal;
}

5. //Calculates taxes on tuition.
float calcTAXES(float FlatTuition)
{
float TaxTotal = 0.0;
TaxTotal = FlatTuition * TAXES;
TaxTotal = TaxTotal * 100 + .05;
TaxTotal = ceil(TaxTotal);
TaxTotal = TaxTotal / 100;
return TaxTotal;
}
Not sure just what the problem is. It may depend on where you do your rounding. If the lines:
TaxTotal = TaxTotal * 100 + .05;
TaxTotal = ceil(TaxTotal);
are an attempt to round off as in the above post, you've got it wrong. If you add .5, use floor(). If you subtract .5, use ceil().
Also, you may want to declare TaxTotal as:
float TaxTotal = 0.0f;
Some systems won't recognize TaxTotal as a float without the f in the declaration, and will convert it to a double automatically. This may not hurt, but might as well be explicit.
ceil() will always round up, so if you add .5 to 525.70, for example, you'll get 526.20. Using ceil() on this will give you 527.00, when you really wanted 526. That's why you use floor() when you add .5, and ceil if you subtract .5
Another possible source of error may be when you divide TaxTotal by 100. Although TaxTotal is rounded to a whole number, it's still a float, with a value of, say, 1953.00, not 1953. You then divide by an int, 100. That could cause type casting errors. You might try dividing by 100.00 (a double), or declare a constant float x =100.00, and divide TaxTotal by x.
Some of this stuff may be compiler dependant, you may not need it all. Don't know why the number of classes taken would make a difference, if the above doesn't help try posting that code.

6. ## Rounding Numbers accurately

I see where you going..sorta..lol!! Well, I came up with a solution and I would like someone to look it over. Can I post the code straight on here. It is a good amount. I like someone to see if I have any holes I am not catching at this moment. By the way when I used floor instead of ceiling it didn't do what I needed but I did come up with away to get around it.