Chrome(谷歌)浏览器或使用谷歌内核的浏览器会出现这个问题
alert("0.035修复前:" + 0.035.toFixed(2));//0.04
alert("0.045修复前:" + 0.045.toFixed(2));//0.04
//修复toFixed的bug
Number.prototype.toFixed = function (fractionDigits) {
var num = this; //console.log(num, fractionDigits);
var f = Math.round(num * Math.pow(10, fractionDigits)) / Math.pow(10, fractionDigits);
var s = f.toString();
if (fractionDigits > 0) {
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + fractionDigits) {
s += '0';
}
}
return s;
};
alert("0.035修复后:" + 0.035.toFixed(2));//0.04
alert("0.045修复后:" + 0.045.toFixed(2));//0.05
觉得有用的话,点个赞吧!