JavaScript Number toFixed() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

number.toFixed(digits) returns a string of the number in fixed-point form with a set count of digits after the decimal. It rounds when needed and pads with zeros when the value is shorter.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call number.toFixed. Start with this line.

JavaScript
n.toFixed([digits])
CallWhat it does
n.toFixed([digits])Primary signature.
number.toFixed(...)Call with the arguments your case needs.

Built into JavaScript. Call toFixed(...) on a number (or Number instance) — no import required.

Library declarations
JavaScript
n.toFixed([digits])

Parameters

ParameterDescription
digitsOptional. Places after decimal (0–100; default 0)

Return Value

Return ValueDescription
*Fixed-point string

JavaScript Examples

Example 1 — Round to Decimal Places

Omit digits for zero places, or pass 2 for money-style display.

JavaScript
const n = 5.56789;

console.log(n.toFixed());     // "6"
console.log(n.toFixed(2));    // "5.57"
console.log(typeof n.toFixed(2));  // "string"
Try It Yourself

How It Works

  1. Default is 0. No argument means round to a whole number → "6".
  2. Two places. toFixed(2) keeps two digits after . and rounds.
  3. Round up. The third decimal is 7, so 5.56 becomes 5.57.
  4. String result. typeof is "string" — display-ready, not for more math.

Example 2 — Pad Trailing Zeros

Whole numbers still get the decimal places you ask for.

JavaScript
console.log((123).toFixed(2));   // "123.00"
console.log((12.5).toFixed(2));  // "12.50"
console.log((1).toFixed(3));     // "1.000"
Try It Yourself

How It Works

  1. Ask for 2 places. Even 123 becomes "123.00".
  2. Partial fraction. 12.5 pads to "12.50".
  3. Three places. (1).toFixed(3) → "1.000".
  4. UI consistency. Padding keeps columns and price fields aligned.

Example 3 — Negatives Need Parentheses

Group the number so the method call applies to the whole value.

JavaScript
console.log((-2.34).toFixed(1));   // "-2.3"  (string)
console.log(typeof (-2.34).toFixed(1));  // "string"

console.log(typeof -2.34.toFixed(1));    // "number"
// unary minus applied AFTER toFixed → number -2.3
Try It Yourself

How It Works

  1. With parentheses. (-2.34).toFixed(1) formats the negative value → string "-2.3".
  2. Type is string. Correct for display.
  3. Without parentheses. -2.34.toFixed(1) runs toFixed on 2.34, then applies unary -.
  4. Surprise type. That path yields a number -2.3, not a string.

Browser & Runtime Support

Number.prototype.toFixed() is part of core JavaScript (ES3+) and is Baseline Widely available.

✓ Baseline · ES3+

Number.toFixed()

Supported in every browser and Node.js version. No polyfill needed for fixed-decimal formatting.

99% 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
toFixed Excellent

Bottom line: Safe everywhere. Ideal for display decimals; use a money library when financial rounding rules matter.

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.

Next: toPrecision()

Learn how toPrecision() formats by total significant digits.

toPrecision() tutorial →

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.

12 people found this page helpful