Is there a way to proper round the number 35.855 to have two decimals in javascript? Trying to round it has different solutions based on the environment (35.85 in Chrome, 35.86 in IE9).
I was looking for a solution at which javascript does wrong rounding for the decimals (rounding .5 values down instead up) and haven’t found a solution that covered all the values that I had. They behaved other in different environments / browsers.
console.log(Math.round(35.855*100)/100);
// result: 35.85 (IE9 : 35.86, Chrome: 35.85 ???)
console.log((35.855).toFixed(2));
// result: 35.85
2
Answers
My solution is just to add the same digit at the end of the number (if there are more than 1 digit), in order to force the previous number to round properly.
Make rounding of 1 character after the decimal part:
And a benchmark: