Fix RangeError: Precision is out of range...

Learn how to resolve the RangeError in JavaScript. toFixed() digits argument must be between 0 and 10...

javascriptrangeerrordebugging

The Error

The RangeError: Precision is out of range error occurs when the JavaScript engine encounters an invalid or out-of-range value for a numerical operation, specifically when using the toFixed() method. This error is thrown because the specified number of digits is outside the allowed range of 0 to 100.

Why it occurs

This error commonly occurs when using the toFixed() method to format a number with a specified number of digits after the decimal point. If the specified number of digits is less than 0 or greater than 100, the JavaScript engine will throw a RangeError.

Example Code

The following code snippet demonstrates how this error can occur:

let num = 123.456789;
console.log(num.toFixed(101)); // throws RangeError: Precision is out of range
console.log(num.toFixed(-1)); // throws RangeError: Precision is out of range

In the above example, the toFixed() method is called with an argument of 101, which is outside the allowed range of 0 to 100. Similarly, calling toFixed() with a negative argument, such as -1, also results in a RangeError.

How to Fix

To fix this error, ensure that the digits argument passed to the toFixed() method is within the valid range of 0 to 100. Here’s a step-by-step solution:

  1. Review the code and identify the toFixed() method call that’s causing the error.
  2. Check the value of the digits argument being passed to toFixed().
  3. If the value is less than 0, adjust it to be 0 or a positive integer.
  4. If the value is greater than 100, adjust it to be 100 or a smaller positive integer.
  5. Update the code with the corrected digits value.

Example:

let num = 123.456789;
console.log(num.toFixed(100)); // valid, rounds to 100 decimal places
console.log(num.toFixed(0)); // valid, rounds to 0 decimal places

Best Practices

To avoid this error in the future, follow these best practices:

Found a typo? Edit this page on GitHub

Related Errors