toFixed() (. Álvaro González ', ).
Chrome Firefox toFixed() :
35.655.toFixed(2) // Yields "36.66" (correct)
35.855.toFixed(2) // Yields "35.85" (wrong, should be "35.86")
MDN :
(function() {
function decimalAdjust(type, value, exp) {
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
Math.round10(55.55, -1);
Math.round10(55.549, -1);
Math.round10(55, 1);
Math.round10(54.9, 1);
Math.round10(-55.55, -1);
Math.round10(-55.551, -1);
Math.round10(-55, 1);
Math.round10(-55.1, 1);
Math.round10(1.005, -2);
Math.floor10(55.59, -1);
Math.floor10(59, 1);
Math.floor10(-55.51, -1);
Math.floor10(-51, 1);
Math.ceil10(55.51, -1);
Math.ceil10(51, 1);
Math.ceil10(-55.59, -1);
Math.ceil10(-59, 1);