由于不同浏览器toFixed四舍五入的处理会有差异,所以改写下。
Number.prototype.toFixed = function (s) { const adjust = this > 0 ? 0.5 : -0.5; let numThe = (parseInt(this * Math.pow(10, s) + 0.5 )/ Math.pow(10, s)).toString(); let index = numThe.indexOf('.'); if (index < 0 && s > 0) { numThe = numThe + '.'; for(let i = 0; i< s; i++){ numThe = numThe + '0'; } } else { index = numThe - index; for (let i = 0; i < (s - index) + 1; i++) { numThe = numThe + '0' } } return numThe;}adjust是对数字本身正负数有个判断