toFixed()
method formats a number using fixed-point notation.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
numObj.toFixed([digits])
digits
可选
0
and
20
, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as
0
.
A string representing the given number using fixed-point notation.
RangeError
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
Number
.
toFixed()
returns a string representation of
numObj
that does not use exponential notation and has exactly
digits
digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If the absolute value of
numObj
is greater or equal to
1e+21
, this method simply calls
Number.prototype.toString()
and returns a string in exponential notation.
警告:
Floating point numbers cannot represent all decimals precisely in binary. This can lead to unexpected results, such as
0.1 + 0.2 === 0.3
returning
false
.
toFixed
let numObj = 12345.6789 numObj.toFixed() // Returns '12346': note rounding, no fractional part numObj.toFixed(1) // Returns '12345.7': note rounding numObj.toFixed(6) // Returns '12345.678900': note added zeros (1.23e+20).toFixed(2) // Returns '123000000000000000000.00' (1.23e-10).toFixed(2) // Returns '0.00' 2.34.toFixed(1) // Returns '2.3' 2.35.toFixed(1) // Returns '2.4'. Note it rounds up 2.55.toFixed(1) // Returns '2.5'. Note it rounds down - see warning above -2.34.toFixed(1) // Returns -2.3 (due to operator precedence, negative number literals don't return a string...) (-2.34).toFixed(1) // Returns '-2.3'
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Number.prototype.toFixed' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toFixed
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | Opera 7 | Safari 2 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Number
Number.isFinite()
Number.isInteger()
Number.isNaN()
Number.isSafeInteger()
Number.parseFloat()
Number.parseInt()
Number.prototype.toExponential()
Number.prototype.toFixed()
Number.prototype.toLocaleString()
Number.prototype.toPrecision()
Number.prototype.toSource()
Number.prototype.toString()
Number.prototype.valueOf()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()