References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript number.toFixed() method

Method JavaScript All modern browsers Updated
Quick answer

The toFixed() method formats a number with a fixed number of decimal places and returns it as a string. (19.5).toFixed(2) gives "19.50". It keeps trailing zeros, which makes it ideal for prices and percentages. Remember the result is a string, so convert back with Number() if you need to keep calculating.

Overview

toFixed() formats a number to a set number of decimal places. (3.14159).toFixed(2) returns "3.14", and crucially it pads with zeros: (5).toFixed(2) is "5.00". That trailing-zero behavior is exactly why it's the go-to for displaying money, percentages and ratings, where $5 should read as $5.00.

The key thing to internalize is that it returns a string, not a number. That's perfect for display, but if you need to do more math you must convert it back with Number(...) or unary +. Compared with Math.round(), which returns a number and drops trailing zeros, toFixed() is the formatting tool and Math.round() the calculation tool.

One caveat: toFixed() rounds, but binary floating point can occasionally make rounding land where you don't expect ((1.005).toFixed(2) is famously "1.00", not "1.01"). For exact currency it's safest to work in integer cents and format only at the end. For locale-aware formatting with thousands separators and currency symbols, Intl.NumberFormat or toLocaleString() is the more capable option.

Syntax

num.toFixed(digits)

(19.5).toFixed(2)     // "19.50"
(3.14159).toFixed(2)  // "3.14"
(5).toFixed(2)        // "5.00"

Parameters

The number.toFixed() method accepts the following parameters.

Parameter Description
digits The number of digits to appear after the decimal point (0-100). Defaults to 0.

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const total = 5;
  const rate = 0.825;

  document.getElementById('out').textContent =
    'price: $' + total.toFixed(2) + '\n' +
    'rate:  ' + (rate * 100).toFixed(1) + '%';
  // price: $5.00 / rate: 82.5%
</script>

Best practices

  • Use it to display money and percentages with consistent decimal places.
  • Remember it returns a string — convert with Number() if you need to calculate further.
  • For locale formatting (separators, currency symbols), use Intl.NumberFormat or toLocaleString().
  • For exact currency math, work in integer cents and format only at the end.

Frequently asked questions

Does toFixed() return a number or a string?
A string. Convert it back with Number(x.toFixed(2)) or +x.toFixed(2) if you need a number.
How do I format a price in JavaScript?
Use price.toFixed(2) for two decimals (e.g. "19.50"), or Intl.NumberFormat for currency symbols and separators.
What is the difference between toFixed() and Math.round()?
toFixed() returns a string with fixed decimals (keeping trailing zeros); Math.round() returns a number rounded to the nearest integer.
Why does toFixed() sometimes round wrong?
Binary floating point can't represent some decimals exactly, so edge cases like (1.005).toFixed(2) may round unexpectedly. Work in integer cents for exact money.