The toPrecision()
method returns a string representing the Number
object to the specified precision.
The toPrecision()
method returns a string representing the Number
object to the specified precision.
toPrecision() toPrecision(precision)
precision
Optional
An integer specifying the number of significant digits.
A string representing a Number
object in fixed-point or exponential notation rounded to precision
significant digits. See the discussion of rounding in the description of the Number.prototype.toFixed()
method, which also applies to toPrecision()
.
If the precision
argument is omitted, behaves as Number.prototype.toString()
. If the precision
argument is a non-integer value, it is rounded to the nearest integer.
RangeError
If precision
is not between 1
and 100
(inclusive), a RangeError
is thrown. Implementations are allowed to support larger and smaller values as well. ECMA-262 only requires a precision of up to 21 significant digits.
toPrecision
let numObj = 5.123456 console.log(numObj.toPrecision()) // logs '5.123456' console.log(numObj.toPrecision(5)) // logs '5.1235' console.log(numObj.toPrecision(2)) // logs '5.1' console.log(numObj.toPrecision(1)) // logs '5' numObj = 0.000123 console.log(numObj.toPrecision()) // logs '0.000123' console.log(numObj.toPrecision(5)) // logs '0.00012300' console.log(numObj.toPrecision(2)) // logs '0.00012' console.log(numObj.toPrecision(1)) // logs '0.0001' // note that exponential notation might be returned in some circumstances console.log((1234.5).toPrecision(2)) // logs '1.2e+3'
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
toPrecision |
1 |
12 |
1 |
5.5 |
7 |
2 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision