鉴于toFixed的银行家舍入法问题,math.round得到的是整数,math.floor是向下取整,Math.ceil是向上取整,也都不符合,所以我特意封装一个简单的方法,来实现四舍五入并且可以保留指定位数小数点:
function toFixed(number, length) {
return (Math.round(Math.pow(10, length) * number) /
Math.pow(10,length)).toFixed(length)
}
欢迎大家测试指正!