From http://www.w3schools.com (Copyright Refsnes Data)

JavaScript toPrecision() Method


Number Object Reference JavaScript Number Object

Definition and Usage

The toPrecision() method formats a number to a specified length.

A decimal point and "0"s are added (if needed), to create the specified length.

Syntax

NumberObject.toPrecision(digits)

Parameter Description
digits Required. The number of digits


Example

Example

In this example we will change number of digits for a number:

<script type="text/javascript">

var num=13.3714;
document.write(num.toPrecision(1)+"<br />")
document.write(num.toPrecision(3)+"<br />")
document.write(num.toPrecision(10))

</script>

The output of the code above will be:


Try it yourself »


Number Object Reference JavaScript Number Object

From http://www.w3schools.com (Copyright Refsnes Data)