JavaScript toFixed(): Round Numbers to Decimal Places

Learn JavaScript toFixed() with rounding examples, string return values, precision gotchas, and how to convert the result back to a number.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

JavaScript toFixed(): Round Numbers to Decimal Places

toFixed() converts a number to a string and rounds it to a specified number of decimal places. It is useful for prices, measurements, reports, and formatted output where two decimal places or another fixed precision is required.

Remember that toFixed() returns a string, not a number. If your input starts as text, pair it with parseFloat() before you format the result.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Quick reference

Fact Detail
Return type Always a string
Typical use Display / formatting (e.g. currency to 2 decimals)
Floating point Binary IEEE-754 can surprise rounding—verify critical values

Method 1: Round to two decimal places

javascript
const number = 1.23456;
const str = number.toFixed(2);

console.log("tofixed:", str);
Output

You should see one line logging tofixed: 1.23.

This is the common format for currency and display values.


Method 2: See a Floating Point Rounding Edge Case

javascript
console.log("tofixed-round:", (1.005).toFixed(2));
Output

You should see one line logging tofixed-round: 1.00.

JavaScript numbers use floating-point arithmetic, so some decimal values do not round the way people expect.


Method 3: Convert the Result Back to a Number

javascript
const value = Number((12.345).toFixed(1));
console.log(value);
Output

You should see one line logging 12.3.

Use Number() if you need to continue arithmetic after formatting.


Method 4: Keep in Mind That toFixed Returns a String

javascript
const fixed = (99.99).toFixed(1);
console.log(typeof fixed);
Output

You should see one line logging string.

That matters when you sort, compare, or add the value later.


Common Questions About JavaScript toFixed

What does toFixed return?

It returns a string representation of the number rounded to the requested number of decimal places.

Why does 1.005.toFixed(2) return 1.00?

Floating-point representation can affect rounding results. This is a standard JavaScript number precision issue.

How do I get a number back from toFixed?

Wrap the result with Number() or unary + if you need a numeric value again.

Is toFixed good for money calculations?

It is good for display, but not for solving floating-point precision problems. For currency math, format only at the final output stage.


Summary

Use JavaScript toFixed() when you need a number formatted to a fixed number of decimal places. It rounds the value and returns a string, so convert the result back to a number only when you need to keep calculating. Be careful with floating-point rounding edge cases when formatting money or precision-sensitive data, and use a parser like parseFloat() when the source value starts as a string.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …