MDN文档指路:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
规范指路:https://tc39.es/ecma262/#sec-number.prototype.tofixed
Number.prototype.toFixed()
The toFixed() method formats a number using fixed-point notation.
// toFixed() 方法使用定点表示法来格式化一个数值。
1 | var numObj = 12345.6789; |
Exceptions RangeError If digits is too small or too large. Values between 0 and 100, inclusive, will not cause a RangeError. Implementations are allowed to support larger and smaller values as chosen. TypeError If this method is invoked on an object that is not a Number.
我们可以看到1.toFixed();
报的是SyntaxError
1 | 1..toFixed() |
改成上面三种形式皆可
V8如何处理toFixed的呢
1 | 2.55 - 2.5 |
https://www.jianshu.com/p/361afb9c4d59