jsguides

Number.prototype.toFixed()

toFixed(digits)

toFixed() formats a number to a specified number of decimal places, returning the result as a string. This is essential for financial calculations, currency display, and anywhere precise decimal control matters.

Syntax

num.toFixed(digits)

Parameters:

  • digits — Number of decimal places (0 to 100). Values 0-20 are most widely supported.

Returns: A string representation of the number with exactly digits decimal places.

How it works

toFixed() rounds the number to the specified precision rather than simply truncating it. The last digit is rounded according to standard rounding rules — if the next digit is 5 or greater, it rounds up.

(2.35).toFixed(1)   // "2.4" — rounds up
(2.34).toFixed(1)   // "2.3" — rounds down
(1.235).toFixed(2)  // "1.24" — rounds up
(1.234).toFixed(2)  // "1.23" — rounds down

Why it returns a string

The string return is deliberate. JavaScript’s number type uses IEEE 754 floating-point, which cannot accurately store many decimal values like 0.1 or 0.2. By returning a string, toFixed() preserves the exact decimal representation without floating-point artifacts creeping in. This is why price.toFixed(2) gives you "19.99" as a string you can display or concatenate with a currency symbol directly.

const price = 19.99;
const formatted = price.toFixed(2); // "19.99" (string)
// Use with currency symbols:
'$' + (9.99).toFixed(2)             // "$9.99"

Common use cases

Currency Formatting

Currency display is the most common use for toFixed(). The method always pads with trailing zeros, so 5.5 becomes "5.50" and 100 becomes "100.00". This consistency means your price columns stay aligned and readable, even when raw values have varying decimal lengths.

const amounts = [19.99, 5.5, 100, 0.1];

amounts.forEach(amount => {
  console.log('$' + amount.toFixed(2));
});
// $19.99
// $5.50
// $100.00
// $0.10

Percentage Display

Percentages need one decimal place for most reports: 33.3% reads better than 33.333333333333336%. The function handles the math and the formatting in two clean steps — compute the ratio, then call toFixed(1) on the result. Keep the calculation separate from the display call so you can adjust precision without touching the formula.

function formatPercent(value, total) {
  const percent = (value / total) * 100;
  return percent.toFixed(1) + '%';
}

formatPercent(7, 20)  // "35.0%"
formatPercent(1, 3)  // "33.3%"

Precision Control

Scientific measurements and constants often carry more digits than you want to show. toFixed() lets you truncate the display without altering the underlying value — the original number stays unchanged. Note that toFixed() returns a string, so if you plan to chain further calculations, keep the raw number in a separate variable.

const measurements = [1.23456, 9.8765, 0.001];

console.log(measurements.map(n => n.toFixed(2)));
// ["1.23", "9.88", "0.00"]

console.log((Math.PI).toFixed(4));  // "3.1416"

Limits and Compatibility

DigitsSupportNotes
0-20UniversalWorks in all browsers and environments
21-100VariesMay throw RangeError in older environments

When digits is omitted, it defaults to 0:

(42).toFixed()     // "42"
(42.7).toFixed()   // "43" — rounds to whole number

When to Use toFixed()

toFixed() is the right tool when the output is meant for people, not for more numeric math. Price tags, summaries, charts, and status readouts often need a stable number of decimal places so that the display does not jump around as values change. The string result is a feature here because it preserves the formatted presentation exactly as it should appear.

It is a poor choice for storing values that you plan to add later. Once you convert to a string, you are no longer working with numbers, so any later math needs a parse step. Keep the raw number for calculations and call toFixed() only at the boundary where you render or serialize the result.

Comparing to toPrecision()

toFixed() controls the number of digits after the decimal point. toPrecision() instead controls the total number of significant digits. That difference matters when values can be very large or very small. If you want a consistent currency display, toFixed(2) is usually clearer. If you want to preserve meaning across a wide numeric range, toPrecision() may fit better.

Both methods return strings, so the same caution applies: do your math first, format last. That pattern keeps rounding decisions in one place and avoids double-rounding bugs, especially in reports that aggregate values from several steps.

Gotchas

Floating-Point Precision

Binary floating-point can’t exactly represent all decimals:

(0.3).toFixed(20)  // "0.29999999999999998890"

For critical financial work, consider using integer math or libraries like decimal.js.

Rounding edge cases

Floating-point representation limits can cause toFixed() to round differently than you might expect. Values that cannot be stored exactly in binary — like 2.55 — may round down instead of up when the internal representation is slightly less than the literal you wrote. Always test edge values if exact rounding matters.

(2.55).toFixed(1)  // "2.5" — should be "2.6" but precision limits prevent it

See Also