Description
The toPrecision() method is used to format a number to a specified length.
A decimal point and nulls are added (if needed), to create the specified length.
This method has the form:
number.toPrecision(x)
Parameter | Description |
---|---|
x | (optional) The number of digits. If omitted, it returns the entire number (without any formatting) |
Note: This method does not change the existing strings, it only returns a copy of the joined strings.
Examples
The following example shows the basic use of this method.
<script>
var num = new Number(123.45);
document.write(num.toPrecision());
</script>
This produces the following result:
Browser Support
Firefox | IE | Chrome | Opera | Safari |
---|---|---|---|---|