Skip to main content
Version: 3.28.0

Number.prototype.toFixed()

The toFixed() method formats a number using fixed-point notation.

Syntax

toFixed()
toFixed(digits)

Parameters

  • digits optional
    • : The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive. If this argument is omitted, it is treated as 0.

Return value

A string representing the given number using fixed-point notation.

Exceptions

  • RangeError
    • : If digits is smaller than 0, larger than 100, or is NaN.
  • TypeError
    • : If this method is invoked on an object that is not a Number.

Description

The toFixed() method returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

If the absolute value of numObj is greater or equal to 1021, this method uses the same algorithm as Number.prototype.toString() and returns a string in exponential notation. toFixed() returns "Infinity", "NaN", or "-Infinity" if the value of numObj is non-finite.

The output of toFixed() may be more precise than toString() for some values, because toString() only prints enough significant digits to distinguish the number from adjacent number values. For example:

(1000000000000000128).toString(); // '1000000000000000100'
(1000000000000000128).toFixed(0); // '1000000000000000128'

However, choosing a digits precision that's too high can return unexpected results, because decimal fractional numbers cannot be represented precisely in floating point. For example:

0.3.toFixed(50); // '0.29999999999999998889776975374843459576368331909180'