Format a number into a specified length:
The result of n will be:
The toPrecision() method formats a number to a specified length.
A decimal point and nulls are added (if needed), to create the specified length.
![]()
The toPrecision() method is supported in all major browsers.
| Parameter | Description |
|---|---|
| x | Optional. The number of digits. If omitted, it returns the entire number (without any formatting) |
| Type | Description |
|---|---|
| String | The number formatted to the specified precision |
| JavaScript Version: | 1.5 |
|---|
Format a number to a specified length:
The result of a,b,c, and d will be:
JavaScript Number Object
Your message has been sent to W3Schools.