ES6 - toPrecision()



This method returns a string representing the number object to the specified precision.

Syntax

number.toPrecision( [ precision ] )

Parameter Details

  • precision − An integer specifying the number of significant digits.

Return value

Returns a string representing a Number object in fixed-point or exponential notation rounded toprecision significant digits.

Example

var num = new Number(7.123456);
console.log(num.toPrecision());
console.log(num.toPrecision(1));
console.log(num.toPrecision(2));

Output

7.123456
7
7.1
Advertisements