toPrecision Method

Top  Previous  Next

Formats a number to a specified length.

Syntax

number1.toPrecision(number2)

Arguments

number1A number representing the number to be formatted to a specified length.
number2The number of digits.

Return Value

The number formatted to the specified precision . If the argument is 1, it returns a number which represents the nearest integer of the number (see Math.round()).

Examples

var num= 5.85236;

var n=num.toPrecision(4);   // Returns 5.852