# Thread: Bug in roundNumber javascript function (+ fix)

1. ## Bug in roundNumber javascript function (+ fix)

Hi folks...

There is a bug in the 2nd roundNumber function at http://www.mediacollege.com/internet...ber/round.html.

[ok]
12905.99999 rounded to 2 decimal places = 12906.00

[fail]
12999.99999 rounded to 2 decimal places = 13.00
5999.99999 rounded to 2 decimal places = 6.00
59999.99999 rounded to 2 decimal places = 6.00
599999.99999 rounded to 2 decimal places = 6.00

To fix this issue, the following snipet:

Code:
```...
if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
if (d1 != ".") {
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
} else {
cutoff -= 1;
}
}
}
d1 += 1;
}
...```

should be replaced by:

Code:
```...
if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
while (cutoff > 0 && d1 == 9) {
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
if (isNaN(d1)) {
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
break;
}
}
}
d1 += 1;
}
...```
Hope that helps,

Roger

2. Thanks for the heads up - I'm sure the staff will get it fixed ASAP.

You might be better off emailing them using the form here: http://www.mediacollege.com/home/contact-feedback.html

They tend not to be on the forum for the moment, as they're quite busy.

James

3. Hi James,

Cheers,

Roger

4. Thanks for the bug report and fix. For some reason we've suddenly had a wave of messages about that function with various suggestions. I've put it on my to-do list to sort through them. I'll post back here ASAP.

5. I'm currently on a mission to clear up all the loose ends I've accumulated over the last few years. Today's task was to deal with the JavaScript rounding function and whilst doing so I found this thread I had left hanging. Very sorry about that.

Anyway, to tidy this up I want to mention that I ended up using a completely different approach that was suggested by a few contributors. You can find it here. As always, if you find any bugs or if you have any suggestions for improvements, let me know.

Roger, I've sent you an email but I also wanted to publicly thank you for your efforts in trying to find a solution. Your fix did indeed solve the problem outlined above, but there were a couple of other (rare) instances when the function still wasn't accurate, and I think you'll agree that the new approach is much more elegant (assuming it continues to withstand testing).

6. Dave, I'm really glad you have returned to this issue...

It was a pleasure to digg into the old code trying to identify the problem. I'll be happy to try your new code as soon as I get some free time.

Very thanks again,

Roger

7. Dave, I'm glad you found a better solution...