I don’t know if I am missing something obvious here, but ...
In IE, Opera, and Chrome, I get what I expect from rounding numbers ending in 5:
125 toPrecision(2) => 130 11.5 toPrecision(2) => 12
This is what I expect.
Firefox, however, is a bit more "sophisticated", giving the following:
125 toPrecision(2) => 120
After a little scratching my head, I came to the conclusion that Firefox uses the “rounding” rule, where if a number up to 5 is equal to a number rounded down and if a number up to 5 is an odd number, it is rounded:
0.5 => 0 1.5 => 2 2.5 => 2 3.5 => 4, etc.
I use rounded results to test student engineering solutions with pseudo-randomly generated questions. The input question in Chrome may be h = 1020 mm, but h = 1030 mm in FF, Chrome or Opera.
I need a function to make rounding consistent, i.e. I want 0.0001235 to be rounded to 0.000124, and I want 1234 to be rounded to 1240, so I cannot use the simple num = Math.floor (num + 0.5); To complicate things a bit, I want the students' input variables and answers to be correct for 3 sig digs if the first digit is not 1, in which case I want 4 sig digs:
234.5 => 235 134.5 => 134.5
I hacked a solution for 3 or 4 sig dig depending on the first digit, converting the number to a string and checking the first non-zero, non-decimal point and non-negative character for “1” - not really, but it works. I could do something similar for the rounding problem by checking to see if the rounded digit is 5, but I am wondering if there is an elegant bitwise solution.