Thread: C++ rounding

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    19

    C++ rounding

    Hi,
    I'm a Java programmer, so I got rather confused, when google didn't give me any usable answers for rounding function.
    I need a function, that would round a double value to 3 digts of fractional part.
    Thanx in advance.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    looking for something like this ?
    Code:
       double d = 12.3446;
       d = floor(d * 1000.0 + 0.5 ) / 1000.0;
    Output
    Code:
    12.345

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Thanx

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Sorry for BUMPing the topic, but I didn't actually understand, HOW that function works...Again google didn't help me :\
    How can I round a number to 2 digts?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just follow it one step at a time with the number. First it is 12.3446, then you multiply by 1000 so it becomes 12344.6, then you add 0.5 so it is 12345.1, then you call floor, which removes the decimal part to make it 12345.0, then you divide by 1000 to make it 12.345.

    >> How can I round a number to 2 digts?
    You should be able to figure it out once you understand how the original code works.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Quote Originally Posted by Daved
    Just follow it one step at a time with the number. First it is 12.3446, then you multiply by 1000 so it becomes 12344.6, then you add 0.5 so it is 12345.1, then you call floor, which removes the decimal part to make it 12345.0, then you divide by 1000 to make it 12.345.

    >> How can I round a number to 2 digts?
    You should be able to figure it out once you understand how the original code works.
    The problem is, that I don't know, what the number is...It's a result of a calculation and it might be whatever...

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It doesn't matter what the original number is... follow the steps with any positive number instead of 12.3446, it will still work.

    It works because it uses multiplication by a power of ten to get the appropriate digit to be the first digit to the right of the decimal. Then it adds 0.5 and cuts off that decimal using floor, which is the same as rounding to the nearest integer (think about why). Then it uses division by the same power of ten to get the decimal point back to where it was originally.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    try this

    Here this should do the trick for you.
    Code:
    double myRounder()
    {
    int decimalPlaces = 3;    //the number of decimal places to round to.
    double roundableInput = 12.3446; //The number you want to round
    double roundedOutput = 0;  //The rounded version of our number once the function runs
    roundedOutput = floor(roundableInput * pow(10,decimalPlaces) + 0.5) / pow(10,decimalPlaces);
    return roundedOutput;
    };
    I'm not really qualified to be helping people because I just started a few weeks ago but I think I got this one.

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I appreciate Daved's effort to educate you, dogbert, and instead of spelling it out for you, will try to make his point even clearer.

    If you have 1234.56789 and you want to round to 1 decimal digit you would want to perform the aformentioned algorithm with 12345.6789 and to yield that number you just multiply 1234.56789 times 10. If you want 2 digits rounded, you would somehow transform 1234.56789 into 123456.789 by multiplying times 100. Et cetra. As should start becoming obvious, you multiple with as many zeros (right of the one) to which you wish to round. Determining how that may be accomplished should be your final quest, and it's pretty straightforward.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Can't use just use a cast instead of floor?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rounding off a double
    By C-Dummy in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 11:45 AM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  3. Rounding errors
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2005, 09:11 PM
  4. preventing rounding problems with doubles
    By mccoz in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2004, 09:23 AM
  5. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM