Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Number and BigInt

Number.prototype.toFixed()

Reference for Number.prototype.toFixed(): formatting a number with a fixed count of decimals, rounding behavior, the string result, and the RangeError limits.

toFixed() converts a number into a string with exactly the number of digits after the decimal point that you request, rounding or padding with zeros as needed. It is the quick tool for showing prices, percentages and measurements.

The result is a string, not a number, and the rounding follows the exact binary value stored for the number, which can surprise you with values such as 1.005.

JavaScript
number.toFixed(digits)

Try it

JS Fixed decimals

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Rounding surprises

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
toFixed()
toFixed(digits)
Name Description
digits Optional. Number of digits after the decimal point, an integer from 0 to 100. The default is 0.

Return value: a string representing the number rounded to digits decimals. For absolute values of 1e21 or more, the result is the same as String(number) (exponent notation).

Exceptions: RangeError if digits is below 0 or above 100. TypeError if called on a non-number.

Usage notes

  • It returns a string. Convert back with Number() or the unary plus if you need a number, and remember that trailing zeros then disappear.
  • Rounding uses the stored value. Because most decimals are not exact in binary, (1.005).toFixed(2) is "1.00", not "1.01". For money, keep integer cents.
  • Ties in the exact value round away from zero: (2.5).toFixed(0) is "3" and (-2.5).toFixed(0) is "-3". This differs from Math.round(), which rounds -2.5 to -2.
  • Pads with zeros: (5).toFixed(2) is "5.00".
  • Not locale aware. It always uses a period. For thousands separators or a comma decimal use toLocaleString() or Intl.NumberFormat.
  • NaN and Infinity return "NaN" and "Infinity"; negative zero gives "0.00" style output without a sign.
  • Syntax trap: 5.toFixed(1) is a syntax error. Write (5).toFixed(1) or 5..toFixed(1).
  • Significant digits instead of decimals: use toPrecision().

Browser support

Supported in every browser and JavaScript engine.