Example 1 — Default Digits
Omit the argument to round to an integer string (same as digits = 0).
(5.56789).toFixed(); // "6" How It Works
5.56789 rounds up to 6 with no fractional part in the result string.
Number.prototype.toFixed() formats a number with a fixed count of digits after the decimal point (same API as MDN Number.toFixed). It rounds when needed and pads with zeros. This tutorial covers syntax, floating-point quirks, comparisons, five examples, and try-it labs.
toFixed(digits)
string
0 to 100
0 places
Display only
Everywhere
Prices, scores, and form fields often need a predictable decimal layout — always two places like "12.50", not "12.5" or "12.5000001".
toFixed() builds that string for you: it rounds to the requested places and pads trailing zeros so the fractional part has a fixed length.
This page is part of JavaScript Number Methods. Related formatters include toString(), toExponential(), and toPrecision().
toFixed() MethodCall number.toFixed() or number.toFixed(digits) on a number primitive or Number object. You always get a string; the number itself does not change.
Omit digits (or pass 0) to round to a whole number string. Pass 2 for common two-decimal display. Very large magnitudes (≥ 1021) may fall back to the same style as toString() and use exponential notation.
Integer literals need parentheses or a space before the dot: (123).toFixed(2). Also group negatives: (-2.34).toFixed(1) — without parens, -2.34.toFixed(1) becomes a number, not a string.
General form of Number.prototype.toFixed:
number.toFixed()
number.toFixed(digits) 0.Always a string in fixed-point notation (with the large-magnitude exception noted below). Special cases:
| Value | Returns (example) |
|---|---|
| Default (0 digits) | (5.56789).toFixed() → "6" |
| Two decimals | (5.56789).toFixed(2) → "5.57" |
| Pad zeros | (123).toFixed(2) → "123.00" |
| Negative | (-2.34).toFixed(1) → "-2.3" |
| Tiny magnitude | (1.23e-10).toFixed(2) → "0.00" |
| Huge magnitude | May use exponential form (same idea as toString()) |
Infinity / NaN | "Infinity" / "NaN" |
digits < 0 or > 100.this value (for example a plain object).(5.56789).toFixed(); // "6"
(5.56789).toFixed(2); // "5.57"
(123).toFixed(2); // "123.00"
(-2.34).toFixed(1); // "-2.3"
Number.parseFloat("1.23e+5").toFixed(2); // "123000.00" | Goal | Code |
|---|---|
| Round to integer string | n.toFixed() |
| Two decimal places | n.toFixed(2) |
| Pad trailing zeros | (12.5).toFixed(2) → "12.50" |
| Display money-like text | Number.parseFloat(x).toFixed(2) |
| Scientific form instead | n.toExponential(2) |
| Locale currency | n.toLocaleString(...) (not toFixed) |
Four facts to remember about Number.prototype.toFixed().
stringThe number is never changed
0–100Optional places after decimal
0No fractional part when omitted
BaselineWorks in all modern engines
toFixed() vs toExponential() vs toPrecision()toFixed(d) | toExponential(d) | toPrecision(p) | |
|---|---|---|---|
| Main job | Fixed decimal places | Always scientific form | Significant digits |
| Example for 1234.5 | "1234.50" (d=2) | "1.23e+3" (d=2) | May be plain or expo |
| Best for | UI decimals / prices | Huge & tiny values | Controlled precision |
Examples follow MDN patterns. Use View Output or Try It Yourself for each case.
Default rounding, then control decimal places.
Omit the argument to round to an integer string (same as digits = 0).
(5.56789).toFixed(); // "6" 5.56789 rounds up to 6 with no fractional part in the result string.
Pass 2 for a common display format.
(5.56789).toFixed(2); // "5.57" The third decimal digit is 7, so the second place rounds from 6 up to 7.
Padding, negatives, and parsing before formatting.
Whole numbers still get fractional zeros for a consistent width.
(123).toFixed(2); // "123.00" There are no fractional digits to keep, so toFixed(2) appends .00.
Wrap the negative value in parentheses so the method returns a string.
(-2.34).toFixed(1); // "-2.3" Without parentheses, -2.34.toFixed(1) applies unary minus to the result of 2.34.toFixed(1) and yields a number -2.3, not the string "-2.3".
An MDN-style helper: parse any numeric text, then fix two decimals.
Number.parseFloat("1.23e+5").toFixed(2); // "123000.00" parseFloat turns "1.23e+5" into 123000; toFixed(2) adds .00 for display.
toFixed() Builds the StringMust be a Number primitive or wrapper.
Default 0, or your digits value 0–100.
Round the fraction; add trailing zeros if needed.
Form like "12.50"; original number unchanged.
digits outside 0–100 throws RangeError.(2.55).toFixed(1) is "2.5" in many engines.(0.3).toFixed(50) example).toString().toLocaleString() / Intl.NumberFormat.Number.prototype.toFixed() is Baseline Widely available — part of JavaScript since early ECMAScript editions and supported in every modern browser and Node.js.
Safe for production everywhere. Optional digits 0–100 works consistently across engines.
Bottom line: Use freely for display formatting. Prefer integer cents or decimal libraries when exact financial rounding rules matter.
Number.prototype.toFixed() turns any number into a fixed-decimal string — ideal for prices, tables, and tidy UI values. Remember it returns text, and floating-point quirks still apply under the hood.
Continue with toPrecision() for significant digits, toExponential() for scientific form, or return to the Number methods hub.
(123).toFixed(2)Number.parseFloat before formatting user textNumber(...) only when you need math againIntl.NumberFormat for locale-aware currency labels10.toFixed(2) without space/parens (syntax error)toFixed as bank-accurate money mathNumber.toFixed()Format fixed decimal places for clean display strings.
string
API0–100
Param.00
ZerosFloat rules
QuirkBaseline
SupportFor some large integers, toFixed(0) can show more digits than toString(). MDN notes (1000000000000000128).toFixed(0) vs .toString() — fixed-point formatting and shortest distinguishable string are different goals.
Return to the hub for the growing Number.prototype collection.
8 people found this page helpful