Rounding Numbers accurately

This is a discussion on Rounding Numbers accurately within the C++ Programming forums, part of the General Programming Boards category; I am trying to find a simple solution to rounding numbers in C++. My dilemma is that I am doing ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    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. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    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. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    3
    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. #5
    Unregistered
    Guest

    //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. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 03:24 PM
  3. (C++) How would you go about rounding numbers?
    By jeffcoulter in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2005, 08:47 AM
  4. Rounding numbers
    By OttoDestruct in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2003, 11:24 AM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21