# Thread: JavaScript Rounding Function

1. ## 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.

2. 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()

3. 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.

4. Bugs?

Round: 21.345
Result: 21.34

5. "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. Quote: lake54
"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. ## 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. Rounding 210.345 to 2 decimal places gives 210.34 and not 210.35.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•