Number.toPrecision

toPrecision — Returns a string representation of the specified Number object.

Description

Number.toPrecision([integer precision])

Parameters

Name Description Type Default Optional
precision A numeric value between 1 and 21 specifying the number of significant digits. integer Yes

Return values

The returned value will be of the primitive string type.

Examples

Example #1 – toPrecision example
var n = new Number(1.12345678901234567890); console.log(n.toPrecision()); // 1.1234567890123457 console.log(n.toPrecision(1)); // 1 console.log(n.toPrecision(3)); // 1.12 console.log(n.toPrecision(5)); // 1.1235 console.log(n.toPrecision(8)); // 1.1234568
Example #2 – toPrecision example
var n = new Number(4242); console.log(n.toPrecision()); // 4242 console.log(n.toPrecision(1)); // 4e+3 console.log(n.toPrecision(3)); // 4.24e+3 console.log(n.toPrecision(5)); // 4242.0 console.log(n.toPrecision(8)); // 4242.0000

External references