Categories
JavaScript Answers

How to Fix the ‘RangeError: precision is out of range’ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps.

Fix the ‘RangeError: precision is out of range’ When Developing JavaScript Apps

To fix the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps, we should make sure we pass in a number between 0 and 20 into the toExponential, toFixed or toPrecision methods.

In Edge, the equivalent error is RangeError: The number of fractional digits is out of range or RangeError: The precision is out of range.

In Firefox, the equivalent error is RangeError: precision {0} out of range.

In Chrome, the equivalent error is RangeError: toExponential() argument must be between 0 and 20, RangeError: toFixed() digits argument must be between 0 and 20, or RangeError: toPrecision() argument must be between 1 and 21.

For instance, we shouldn’t write code like:

88.toExponential(-1); 
88.1234.toExponential(101); 

2.34.toFixed(-100);    
2.34.toFixed(1001);        

1234.5.toPrecision(-1);    
1234.5.toPrecision(101);

Instead, we write:

88.toExponential(11); 
88.1234.toExponential(11); 

2.34.toFixed(10);    
2.34.toFixed(10);        

1234.5.toPrecision(1);    
1234.5.toPrecision(11);

Conclusion

To fix the ‘RangeError: precision is out of range’ when we’re developing JavaScript apps, we should make sure we pass in a number between 0 and 20 into the toExponential, toFixed or toPrecision methods.

In Edge, the equivalent error is RangeError: The number of fractional digits is out of range or RangeError: The precision is out of range.

In Firefox, the equivalent error is RangeError: precision {0} out of range.

In Chrome, the equivalent error is RangeError: toExponential() argument must be between 0 and 20, RangeError: toFixed() digits argument must be between 0 and 20, or RangeError: toPrecision() argument must be between 1 and 21.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *