Open In App
Related Articles

JavaScript RangeError Precision is out of range

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

This JavaScript exception precision is out of range occurs if a number that is outside the range of 0 and 20 (or 21) is passed into toFixed(), toPrecision(), or toExponential() method.

Error Message:

RangeError: The number of fractional digits is out of range (Edge) 
RangeError: The precision is out of range (Edge) 
RangeError: precision {0} out of range (Firefox) 
RangeError: toExponential() argument must be between 0 and 20 (Chrome) 
RangeError: toFixed() digits argument must be between 0 and 20 (Chrome) 
RangeError: toPrecision() argument must be between 1 and 21 (Chrome)

Error Type:

RangeError

What Happened?

There is a precision argument that is out of range in one of the methods, toExponential(), toFixed(), and toPrecision().

Example 1: In this example, the RangeError occurred as -100 to toFixed() is passed.

Javascript

function Geeks() {
    try {
        3.54.toFixed(-100);
 
        console.log("'Precision out of range'"
            + " error has not occurred");
    } catch (e) {
        console.log("'Precision out of range'"
            + " error has occurred");
    }
}
 
Geeks();

                    

Output:

'Precision out of range' error has occurred

Example 2: In this example, the argument passed to toExponential() is -4, So the RangeError has occurred.

Javascript

function Geeks() {
    try {
        77.1234.toExponential(-4);
 
        console.log("'Precision out of range'"
            + " error has not occurred");
    } catch (e) {
        console.log("'Precision out of range'"
            + " error has occurred");
    }
}
 
Geeks();

                    

Output:

'Precision out of range' error has occurred

Example 3: In this example, the RangeError occurred as -1 to toPrecision() is passed.

Javascript

function Geeks() {
    try {
        5643.9.toPrecision(-1);
 
        console.log("'Precision out of range'"
            + " error has not occurred");
    } catch (e) {
        console.log("'Precision out of range'"
            + " error has occurred");
    }
}
 
Geeks();

                    

Output:

'Precision out of range' error has occurred


Last Updated : 28 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads