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, check if the string is numeric before you parse it with parseFloat() and call toFixed() on 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
const number = 1.23456;
const str = number.toFixed(2);
console.log("tofixed:", str);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
console.log("tofixed-round:", (1.005).toFixed(2));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. That is the same IEEE-754 family of surprises as Math.fround()—toFixed() formats for display, while fround() targets 32-bit float spacing, not currency rounding.
Method 3: Convert the Result Back to a Number
const value = Number((12.345).toFixed(1));
console.log(value);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
const fixed = (99.99).toFixed(1);
console.log(typeof fixed);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 validate or parse string inputs before formatting as shown in the introduction.
