JavaScript Number toPrecision() Method
-
Syntax of JavaScript Number
toPrecision()
Method -
Example 1: Use the
number.toPrecision()
Method Withoutdigits
Parameter -
Example 2: Use the
number.toPrecision()
Method Withdigits
Parameter - Exceptions
-
Example 3: Use the
number.toPrecision()
Method Withdigits
Values Not Between 1 to 100 -
Example 4: Invoke the
number.toPrecision()
Method on String Values
Users can use the number.toPrecision()
method to format the Number
object to particular precision. Furthermore, the method converts the Number
object to a string and formats the Number
object.
Syntax of JavaScript Number toPrecision()
Method
let number = 23.2321;
number.toPrecision();
number.toPrecision(digits);
Parameters
digits
- The digits
is an optional parameter specifying the total number of digits you want to keep in the Number
object string, including before and after the decimal point.
Return
The number.toPrecision()
method returns the Number
object in the string format after formatting to the digits
precision.
Example 1: Use the number.toPrecision()
Method Without digits
Parameter
We have taken different positive and negative numeric values in the example below. We have used the number.toPrecision()
method to format the number without the digits
parameter.
In the output, users can observe that when we don’t pass the digits
parameter, it returns the same Number
object after converting it to a string.
let num_value1 = 123.132;
let num_value2 = -2311;
console.log(num_value1.toPrecision());
console.log(num_value2.toPrecision());
Output:
123.132
-2311
Example 2: Use the number.toPrecision()
Method With digits
Parameter
When we use the digits
parameter with the number.toPrecision()
method, it formats the number string to contain only the total digits
number.
Here, we have to use the exponential notation of the number for the value3
number object, and users can see how it is formatted using the number.toPrecision()
method in the example below.
Also, the number.toPrecision()
method neglects the leading zero and removes it, which you can see in the output of value4
.
let value1 = 1987.132432;
let value2 = -2.112;
let value3 = 123.34e+5;
let value4 = 00090;
console.log(value1.toPrecision(4));
console.log(value2.toPrecision(6));
console.log(value3.toPrecision(2));
console.log(value4.toPrecision(5));
Output:
1987
-2.11200
1.2e+7
90.000
Exceptions
Exceptions | Description |
---|---|
RangeError |
When the value of digits is outside 1 and 100, the number.toPrecision method returns the RangeError . |
TypeError |
Invoking the number.toPrecision() method on the other object rather than the Number object creates a TypeError . |
Example 3: Use the number.toPrecision()
Method With digits
Values Not Between 1 to 100
If users follow the ECMA-262 script to format the numbers, they need a precision of up to 21 digits. Still, the number.toPrecision()
method allows us to format the number up to 100 precision.
When we pass the value of digits
above 100 or negative, it always returns the RangeError
, which users can understand using the example below.
let value1 = 1987.132432;
let value2 = -2.112;
console.log(value1.toPrecision(300));
console.log(value2.toPrecision(-10));
Output:
RangeError: toPrecision() argument must be between 1 and 100
Example 4: Invoke the number.toPrecision()
Method on String Values
The number.toPrecision()
method works only with number values. If we invoke it with string or other objects, it returns TypeError
.
To solve the error, users can parse the number from the string.
In the example below, we have used the string objects with the number.toPrecision()
method to demonstrate the TypeError
.
let value1 = "Welcome!";
let value2 = "Delft";
console.log(value1.toPrecision());
console.log(value2.toPrecision());
Output:
TypeError: value1.toPrecision is not a function
This article has demonstrated the different use cases of the number.toPrecision()
method. Also, we have learned to overcome the errors and exceptions we get while using the method.