JavaScript Number toFixed() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Fixed decimals

What You’ll Learn

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.

01

Syntax

toFixed(digits)

02

Returns

string

03

Digits

0 to 100

04

Default

0 places

05

UI / money

Display only

06

Baseline

Everywhere

Introduction

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().

Understanding the toFixed() Method

Call 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.

💡
Beginner Tip

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.

📝 Syntax

General form of Number.prototype.toFixed:

JavaScript
number.toFixed()
number.toFixed(digits)

Parameters

  • digits (number, optional) — integer from 0 to 100. Digits after the decimal point. Default 0.

Return value

Always a string in fixed-point notation (with the large-magnitude exception noted below). Special cases:

ValueReturns (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 magnitudeMay use exponential form (same idea as toString())
Infinity / NaN"Infinity" / "NaN"

Exceptions

  • RangeErrordigits < 0 or > 100.
  • TypeError — called on a non-Number this value (for example a plain object).

Common patterns

JavaScript
(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"

⚡ Quick Reference

GoalCode
Round to integer stringn.toFixed()
Two decimal placesn.toFixed(2)
Pad trailing zeros(12.5).toFixed(2)"12.50"
Display money-like textNumber.parseFloat(x).toFixed(2)
Scientific form insteadn.toExponential(2)
Locale currencyn.toLocaleString(...) (not toFixed)

🔍 At a Glance

Four facts to remember about Number.prototype.toFixed().

Returns
string

The number is never changed

Digits
0–100

Optional places after decimal

Default
0

No fractional part when omitted

Support
Baseline

Works in all modern engines

📋 toFixed() vs toExponential() vs toPrecision()

toFixed(d)toExponential(d)toPrecision(p)
Main jobFixed decimal placesAlways scientific formSignificant digits
Example for 1234.5"1234.50" (d=2)"1.23e+3" (d=2)May be plain or expo
Best forUI decimals / pricesHuge & tiny valuesControlled precision

Examples Gallery

Examples follow MDN patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Default rounding, then control decimal places.

Example 1 — Default Digits

Omit the argument to round to an integer string (same as digits = 0).

JavaScript
(5.56789).toFixed();  // "6"
Try It Yourself

How It Works

5.56789 rounds up to 6 with no fractional part in the result string.

Example 2 — Two Decimal Places

Pass 2 for a common display format.

JavaScript
(5.56789).toFixed(2);  // "5.57"
Try It Yourself

How It Works

The third decimal digit is 7, so the second place rounds from 6 up to 7.

📈 Practical Patterns

Padding, negatives, and parsing before formatting.

Example 3 — Pad Trailing Zeros

Whole numbers still get fractional zeros for a consistent width.

JavaScript
(123).toFixed(2);  // "123.00"
Try It Yourself

How It Works

There are no fractional digits to keep, so toFixed(2) appends .00.

Example 4 — Negative Numbers

Wrap the negative value in parentheses so the method returns a string.

JavaScript
(-2.34).toFixed(1);  // "-2.3"
Try It Yourself

How It Works

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".

Example 5 — Parse Then Format

An MDN-style helper: parse any numeric text, then fix two decimals.

JavaScript
Number.parseFloat("1.23e+5").toFixed(2);  // "123000.00"
Try It Yourself

How It Works

parseFloat turns "1.23e+5" into 123000; toFixed(2) adds .00 for display.

🚀 Common Use Cases

  • Price labels — show two decimals in carts and invoices (display only).
  • Form inputs — normalize user numbers before showing them back.
  • Tables & dashboards — align columns with a fixed fractional width.
  • Scores & percentages — clip noisy floats for the UI.
  • CSV / export — consistent decimal text in plain files.
  • Not bank-grade math — use integer cents or a decimal library for real money rules.

🧠 How toFixed() Builds the String

1

Read the number

Must be a Number primitive or wrapper.

Input
2

Choose digit count

Default 0, or your digits value 0–100.

Digits
3

Round and pad

Round the fraction; add trailing zeros if needed.

Format
4

Return string

Form like "12.50"; original number unchanged.

📝 Notes

  • Does not change the number — returns a new string.
  • digits outside 0–100 throws RangeError.
  • Floating-point limits can surprise you: (2.55).toFixed(1) is "2.5" in many engines.
  • Very high digit counts can reveal binary fraction noise (see MDN’s (0.3).toFixed(50) example).
  • Magnitudes ≥ 1021 may use exponential notation like toString().
  • For locale currency symbols and grouping, prefer toLocaleString() / Intl.NumberFormat.

Browser & Runtime Support

Number.prototype.toFixed() is Baseline Widely available — part of JavaScript since early ECMAScript editions and supported in every modern browser and Node.js.

Baseline · Widely available

Number.prototype.toFixed()

Safe for production everywhere. Optional digits 0–100 works consistently across engines.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Number.toFixed() Excellent

Bottom line: Use freely for display formatting. Prefer integer cents or decimal libraries when exact financial rounding rules matter.

Conclusion

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.

💡 Best Practices

✅ Do

  • Use parentheses for integers and negatives: (123).toFixed(2)
  • Pick a small digit count for readable UI (often 0–4)
  • Parse with Number.parseFloat before formatting user text
  • Convert back with Number(...) only when you need math again
  • Use Intl.NumberFormat for locale-aware currency labels

❌ Don’t

  • Write 10.toFixed(2) without space/parens (syntax error)
  • Treat toFixed as bank-accurate money math
  • Pass digits below 0 or above 100
  • Forget that the return type is a string
  • Use huge digit counts expecting exact decimal fractions

Key Takeaways

Knowledge Unlocked

Five things to remember about Number.toFixed()

Format fixed decimal places for clean display strings.

5
Core concepts
🔢 02

Digits

0–100

Param
. 03

Pads

.00

Zeros
04

Rounds

Float rules

Quirk
05

Safe

Baseline

Support

❓ Frequently Asked Questions

Number.prototype.toFixed() returns a string of the number in fixed-point notation with a chosen count of digits after the decimal point. Example: (5.56789).toFixed(2) is "5.57".
digits is an optional integer from 0 to 100 for how many places appear after the decimal point. If you omit it, JavaScript treats it as 0. Values outside 0–100 throw a RangeError.
Always a string. That is why (123).toFixed(2) is "123.00" — useful for display and forms, but you must convert again if you need to do more math.
toFixed() keeps a fixed count of decimal places in plain form (except for huge magnitudes). toExponential() always uses scientific notation. toPrecision() controls significant digits and may use either form.
JavaScript numbers are floating-point. Some decimals cannot be stored exactly, so (2.55).toFixed(1) may be "2.5" and very large digit counts can show long binary artifacts. Prefer a money library for financial rounding rules.
It is part of early ECMAScript and is Baseline Widely available — safe in every modern browser and Node.js.
Did you know?

For 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.

More Number Methods

Return to the hub for the growing Number.prototype collection.

Number methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful