toFixed 定义:toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。
let a1= 3.335 a1.toFixed(2) // "3.33"
结果应该是3.34
这是为什么???mozilla toFixed解释
toFixed 是银行家进位法,不是四舍五入。什么叫银行家进位法,你可以自己搜索一下。
toFixed
JS 没提供内置的四舍五入方法,可以利用 Math.round 自己实现一个。
Math.round
下面是一个参考例子:
// Javascript 源码https://github.com/fudiwei/st...// Typescript 声明https://github.com/fudiwei/st...
2.1m questions
2.1m answers
63 comments
56.6k users