I don't know if I'm missing something obvious here but...
In IE, Opera and Chrome, I get what I expect from rounding numbers ending in a 5:
125 toPrecision(2) => 130
11.5 toPrecision(2) => 12
This is what I'd expect.
Firefox, though, is a little more 'sophisticated' yielding the following:
125 toPrecision(2) => 120 //wtf!!!
11.5 toPrecision(2) => 12
After a bit of head scratching, I've come to the conclusion that Firefox is using a 'rounding even' rule where, if the digit before the 5 is even the number rounds down and if the digit before the 5 is odd the number rounds up:
0.5 => 0
1.5 => 2
2.5 => 2
3.5 => 4, etc.
I am using the rounded results to test student solutions to engineering questions with pseudo-randomly generated question inputs. A question input in Chrome could 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 round up to 0.000124 and I want 1234 to round to 1240 so I can't use a simple num = Math.floor(num + 0.5); To complicate matters a bit, I want input variables and student answers to be correct to 3 sig digs unless the first digit is a 1, in which case I want 4 sig digs:
234.5 => 235
134.5 => 134.5
I've hacked a solution to the 3 or 4 sig digs depending upon the first digit by converting the number to a string and testing the first non-zero, non-decimal point and non-negative character for '1' - not pretty, but it works. I could do something similar for the rounding problem, checking whether the digit to be rounded is a 5 but I'm wondering if there is an elegant bit-wise solution.