重写toFixed()方法

1、直接在原型上修改,仍然使用元调用方式

Number.prototype.toFixed = function (exponent) {
    return parseInt(this * Math.pow(10, exponent) + 0.5) / Math.pow(10, exponent); 
}

2、使用方法调用

function toFixed(num, s) {
    var times = Math.pow(10, s)
    var des = num * times + 0.5
    des = parseInt(des, 10) / times
    return des + ''
}
原文地址:https://www.cnblogs.com/tkpn/p/10722464.html