Results 1 to 8 of 8
  1. #1
    Administrator Dave's Avatar
    Join Date
    Sep 2004
    Location
    Te Awamutu, New Zealand
    Posts
    3,959
    Blog Entries
    79

    JavaScript Rounding Function

    This thread exists to discuss our attempt to create an accurate decimal rounding function for JavaScript. You can find the function at http://www.mediacollege.com/internet...ber/round.html

    Why? Because JavaScript's built-in Math.round() function isn't 100% accurate and we're trying to do better.

    Please use this thread to report any bugs, suggestions or other feedback. Thanks.
    Dave Owen
    MediaCollege.com

  2. #2
    Administrator Dave's Avatar
    Join Date
    Sep 2004
    Location
    Te Awamutu, New Zealand
    Posts
    3,959
    Blog Entries
    79
    Bug #1: Errors start creeping in with very large numbers, e.g. 21112222333341234.5

    I've noticed that these numbers also don't round correctly with Math.round()
    Dave Owen
    MediaCollege.com

  3. #3
    Administrator Dave's Avatar
    Join Date
    Sep 2004
    Location
    Te Awamutu, New Zealand
    Posts
    3,959
    Blog Entries
    79
    Issue: The function does not take into consideration digits further down, e.g. 21.344444445 rounds to 21.34 and not 21.35

    I haven't listed this as a bug because I believe this is more an issue of rounding convention, and my understanding is that most conventions would return 21.34 as the correct answer (Math.round certainly does). However I'm not a mathematician so I'll leave this open for discussion amongst those more knowledgeable than me.

    Thanks to Veli Pehlivanov for pointing this out. Veli has provided the following possible solution:

    Code:
    function roundTo(num, dec)
    {
       var ret = num;
       var counter = 0;
    
       var powVal = num * Math.pow(10, dec);
       if(powVal / Math.round(powVal) == 1)
       {
           return num;
       }
    
       do
       {
           ret = ret * 10;
           counter++;
       }while(ret % 10 > 0);
    
       while(counter> dec)
       {
           ret = Math.round(ret / 10);
           counter--;
       }
    
       return ret / Math.pow(10, dec);
    }
    This apparently works precisely for up to 16 decimal digits.
    Dave Owen
    MediaCollege.com

  4. #4
    Bugs?

    Round: 21.345
    Result: 21.34

  5. #5
    Camera Operator/Producer lake54's Avatar
    Join Date
    Jun 2009
    Location
    Manchester, UK
    Posts
    384
    "21.344444445 rounds to 21.34 and not 21.35" - that is mathematically correct. You only look at the next number to what you want to round it to. If it is 0-4, keep it as-is. If it is 5 or higher, round up.

  6. #6
    Quote
    Quote: lake54
    View Post
    "21.344444445 rounds to 21.34 and not 21.35" - that is mathematically correct. You only look at the next number to what you want to round it to. If it is 0-4, keep it as-is. If it is 5 or higher, round up.
    Actually the most accurate method is to round from the end of the number to the decimal and isn't it just easier to add .5555555555555 to the number then parseInt that number... this should round it perfectly with no issues even on javascript. i have made a simple random number rounding applet that shows the number and what it was rounded to using this method and so far haven't found any issues. now if you want to round at another point just add 5's to every place after your rounding place then use parse to cut out the rest of the numbers leaving zeros or what ever...

  7. #7
    New Member
    Join Date
    May 2011
    Location
    London
    Posts
    1

    Bug with 0.595, gives 0.59

    Need to have rounding perfected for a financial site, where the number of (fixed) decimal digits can be up to six. Searched around for the ideal solution, but all seem to have bugs. toFixed() was shown to have a problem with 0.595 - and it does on your demo. round() gives 0.6 (correct) but toFixed() gives 0.59 (incorrect).

    Looks at the moment that a better solution, for this app at least (don't need scientific notation or very large numbers) is to use toRound() and then pad with zeroes. Noted in my tests though, that 1.00 rounds to 1. - with a trailing decimal point! So even more string manipulation required.

    Anyone found the perfect solution yet? Not only that, as the numbers will be updating rapidly, and hundreds of them, we need it to work fast!

    You mentioned that Math.round() also has bugs, can you give some test cases?

  8. #8
    New Member
    Join Date
    Jul 2011
    Location
    London
    Posts
    1
    Rounding 210.345 to 2 decimal places gives 210.34 and not 210.35.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Subscribe to us on YouTube