Skip to content

RangeError: toFixed() digits argument must be between 0 and 100

Quick summary

You passed a negative or very large precision to .toFixed — only 0–100 is allowed.

Why this happens

  • Precision comes from a calculation that went negative

  • Passing a huge number of digits from untrusted input

  • Confusing .toFixed with .toPrecision which has different limits

Minimal example

✗ Broken code
const price = 9.99;
console.log(price.toFixed(-2)); // RangeError
✓ Fixed code
const price = 9.99;
const digits = Math.min(100, Math.max(0, -2));
console.log(price.toFixed(digits)); // '10'

A negative precision is invalid for toFixed. Clamping to 0 falls back to rounding with no decimal places, avoiding the error.

How to diagnose

  • Read the RangeError naming the invalid precision

  • Log the precision argument before the call

  • Clamp the precision to the valid 0–100 range

How to fix

  • Clamp: Math.min(100, Math.max(0, precision))

  • Validate user input before passing to .toFixed

  • Use a default precision when the input is out of range

How to prevent

  • Always validate precision before calling .toFixed

  • Use a constant for the maximum allowed precision

Related Resources

Related Glossary

Related Lessons

← Back to language