Format a number into a specified length:
The result of n will be:
More "Try it Yourself" examples below.
The toPrecision() method formats a number to a specified length.
A decimal point and nulls are added (if needed), to create the specified length.
Method | |||||
---|---|---|---|---|---|
toPrecision() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
x | Optional. The number of digits. If omitted, it returns the entire number (without any formatting) |
Return Value: | A String, representing a number formatted to the specified precision |
---|---|
JavaScript Version: | 1.5 |
Format a number to a specified length:
The result of a,b,c,d will be:
Format a number between 0 and 1:
The result of a,b,c, and d will be: