Number.prototype.toFixed() is a JavaScript method that formats a number using fixed-point notation and returns a string representation with a specified number of digits after the decimal point.

  • Syntax: num.toFixed(digits)

    • digits (optional): An integer between 0 and 100 specifying the number of digits after the decimal point. If omitted, it defaults to 0 (rounds to the nearest integer).

  • Returns: A string representing the number with fixed decimal places. The number is rounded using standard rounding rules, and trailing zeros are added if necessary.

  • Key Points:

    • Always returns a string, not a number. Use Number() or unary + to convert back to a number if needed.

    • Throws a RangeError if digits is less than 0 or greater than 100.

    • Throws a TypeError if the method is called on a non-Number value.

    • For numbers with a magnitude ≥ 10²¹, it returns the result in exponential notation (same as toString()).

    • Known issue: Due to floating-point precision, some calculations may yield unexpected results (e.g., (1.015).toFixed(2) returns "1.01" instead of "1.02"). A polyfill can fix this.

Common Use Cases:

  • Formatting currency values (e.g., price.toFixed(2)).

  • Rounding numbers for display (e.g., 0.1 + 0.2 results in 0.30000000000000004; use toFixed(1) to correct it).

  • Ensuring consistent decimal places in financial or scientific applications.

Edit: To answer your edit, use Math.round. You could also prototype the Number object to have it do your bidding if you prefer that syntax.

Number.prototype.round = function() {
  return Math.round(this);
}
var num = 3.5;
alert(num.round())

I've never used Number.toFixed() before (mostly because most JS libraries provide a toInt() method), but judging by your results I would say it would be more consistent to use the Math methods (round, floor, ceil) then toFixed if cross-browser consistency is what you are looking for.

Answer from TJ L on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toFixed
Number.prototype.toFixed() - JavaScript | MDN
The toFixed() method of Number values returns a string representing this number using fixed-point notation with the specified number of decimal places.
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
JavaScript toFixed() Method
❮ Previous JavaScript Number ... num.toFixed(2); Try it Yourself » · More examples below · The toFixed() method converts a number to a string....
Discussions

javascript - Math.round(num) vs num.toFixed(0) and browser inconsistencies - Stack Overflow
It's still worth reiterating that, even if the specs were implemented consistently across all browsers, the behavior defined in the spec, particularly for toFixed rounding, can still be a bit unintuitive for "mere mortal" JS developers who expect true mathematical accuracy—see Javascript toFixed ... More on stackoverflow.com
🌐 stackoverflow.com
Number toFixed(2) in Js if decimal have - JavaScript - SitePoint Forums | Web Development & Design Community
generally, to fix the number in javascript we write the toFixed(2) function. I want to make a function for numbers when a number has a decimal like 12.326252 it will show 12.33 and when it has no decimal like 123 it will… More on sitepoint.com
🌐 sitepoint.com
0
September 19, 2022
Javascript tofixed
I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
April 5, 2018
This is why you should not using "toFixed" to rounding a number to the nearest integer.
The specification https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.prototype.tofixed is just incomprehensible ... More on reddit.com
🌐 r/JavaScriptTips
4
10
July 13, 2022
🌐
My Life and Times
macengr.wordpress.com › 2016 › 04 › 04 › freecodecamp-and-javascript-the-tofixed-method
FreeCodeCamp and JavaScript: The toFixed() Method – My Life and Times
April 4, 2016 - We didn’t specify a value for digit. toFixed() takes the number and looks at the decimal part – greater than 5, just like we all learned in math class, means you round up – and gives you back the rounded number with no decimal places.
Top answer
1 of 6
41

Edit: To answer your edit, use Math.round. You could also prototype the Number object to have it do your bidding if you prefer that syntax.

Number.prototype.round = function() {
  return Math.round(this);
}
var num = 3.5;
alert(num.round())

I've never used Number.toFixed() before (mostly because most JS libraries provide a toInt() method), but judging by your results I would say it would be more consistent to use the Math methods (round, floor, ceil) then toFixed if cross-browser consistency is what you are looking for.

2 of 6
25

To address your two original issues/questions:

Math.round(num) vs num.toFixed(0)

The issue here lies in the misconception that these should always give the same result. They are, in fact, governed by different rules. Look at negative numbers, for example. Because Math.round uses "round half up" as the rule, you will see that Math.round(-1.5) evaluates to -1 even though Math.round(1.5) evaluates to 2.

Number.prototype.toFixed, on the other hand, uses what is basically equivalent to "round half away from zero" as the rule, according to step 6 of the spec, which essentially says to treat negatives as positive numbers, and then add back the negative sign at the end. Thus, (-1.5).toFixed(0) === "-2" and (1.5).toFixed(0) === "2" are true statements in all spec-compliant browsers. Note that these values are strings, not numbers. Note further that both -1.5.toFixed(0) and -(1.5).toFixed(0) are === -2 (the number) due to operator precedence.

Browser inconsistencies

Most modern browsers—or at least the ones you might be expected to support at the time of this writing except for IE—should all implement the specs correctly. (According to Renee's comment, the toFixed issue you pointed out in Opera has been fixed, presumably since they started using the same JS engine as Chrome.) It's still worth reiterating that, even if the specs were implemented consistently across all browsers, the behavior defined in the spec, particularly for toFixed rounding, can still be a bit unintuitive for "mere mortal" JS developers who expect true mathematical accuracy—see Javascript toFixed Not Rounding and this "works as intended" bug that was filed on the V8 JS engine for examples.

Conclusion

In short, these are two different functions with two different return types and two different sets of rules for rounding.

As others have suggested, I would also like to say "use whichever function fits your particular use case" (taking special care to note the peculiarities of toFixed, especially IE's errant implementation). I would personally lean more towards recommending some explicit combination of Math.round/ceil/floor, again, as others have mentioned. Edit: ...though, after going back and reading your clarification, your use case (rounding to a whole number) definitely calls for the aptly-named Math.round function.

🌐
Medium
dilanivasanthan26.medium.com › different-betwwen-tofixed-toprecision-and-mathround-20ed8edaf78a
Different between toFixed, toPrecision and mathRound | by Dilani Vasanthan | Medium
October 12, 2023 - - `toFixed()` is a method that can be called on a number in JavaScript. - It formats a number to a specified number of decimal places and returns a string. - The specified argument indicates the number of decimal places in the result.
Find elsewhere
🌐
Programiz
programiz.com › javascript › library › number › tofixed
JavaScript Number toFixed()
let num = 57.77583; console.log(num.toFixed()); // 58-> rounded off, no fractional part console.log(num.toFixed(1)); // 57.8 console.log(num.toFixed(7)); // 57.7758300 -> Added zeros console.log(num.toFixed(2)); // 57.78 console.log((5.68e20).toFixed(2)); // 568000000000000000000.00 console.log((1.23e-10).toFixed(2)); // 0.00 console.log((-2.34).toFixed(1)); // -2.3
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Number › toFixed
JavaScript Number toFixed() - Format to Decimal Places | Vultr Docs
December 2, 2024 - The toFixed() method in JavaScript is particularly useful for formatting a number to a specified number of decimal places. This method converts a number into a string, rounding to a fixed number of decimals.
🌐
Flexiple
flexiple.com › javascript › number-tofixed-method
JavaScript Number toFixed() Method - Flexiple
July 11, 2024 - The JavaScript toFixed() method formats a number to a specified number of decimal places. This method returns a string representation of the number that does not round up or down if the specified decimal places are fewer than the number's actual ...
🌐
TechOnTheNet
techonthenet.com › js › number_tofixed.php
JavaScript: Number toFixed() method
In JavaScript, toFixed() is a Number method that is used to convert a number to fixed-point notation (rounding the result where necessary) and return its value as a string.
🌐
Dfkaye
dfkaye.com › posts › 2021 › 07 › 17 › fixing-number.tofixed
Fixing Number.toFixed() | dfkaye.com
July 17, 2021 - Recall earlier that we tested various 3-digit fractions against a fixing length of 2. The rounding error disappears if we add another digit to the fraction, so that, for example, (1.0151).toFixed(2) returns “1.02” as expected.
🌐
Bennadel
bennadel.com › blog › 1013-javascript-number-tofixed-method.htm
Javascript Number.toFixed() Method
April 19, 2020 - There are several ways to convert strings to numbers in JavaScript. One way is the parseFloat that you've shown here, but you can convert it directly into a number with the following: var str = Number('1234.56789'); An alternate method of moving a string to number is to subtract 0 from it: var str = '1234.56789' - 0; alert(str.toFixed(2)); // assuming that the string is actually a number
🌐
SitePoint
sitepoint.com › javascript
Number toFixed(2) in Js if decimal have - JavaScript - SitePoint Forums | Web Development & Design Community
September 19, 2022 - generally, to fix the number in javascript we write the toFixed(2) function. I want to make a function for numbers when a number has a decimal like 12.326252 it will show 12.33 and when it has no decimal like 123 it will show 123, not 123.00. ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Javascript tofixed - JavaScript - The freeCodeCamp Forum
April 5, 2018 - <html> <head> <style> table { border: 1px solid #666; width: 100%; } td { border: 1px solid black; } </style> <script src="http://ajax.googleapis.com/ajax/libs/jquer…
🌐
Codedamn
codedamn.com › news › javascript
toFixed method in JavaScript- What and how to use it?
April 6, 2022 - In JavaScript, the toFixed() method is used to format a number in fixed-point notation.
🌐
TutorialsPoint
tutorialspoint.com › javascript › number_tofixed.htm
JavaScript Number toFixed() Method
The JavaScript Number toFixed() method returns the representation of a numerical value with or without decimal places. It includes an optional parameter called 'digits'. However, if the digits parameter is not within the range of '[0,100]', ...
🌐
Educative
educative.io › answers › what-is-tofixed-in-javascript
What is toFixed() in Javascript?
toFixed() formats a number to a given length after the decimal point by rounding off to reduce the length or by adding zeros to increase the length.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › tofixed
The toFixed() Function in JavaScript - Mastering JS
March 14, 2023 - JavaScript numbers have a toFixed() method that converts a number to a string and rounds the number to a given number of decimal places.
🌐
Codecademy
codecademy.com › docs › javascript › number methods › .tofixed()
JavaScript | Number Methods | .toFixed() | Codecademy
September 3, 2021 - The .toFixed() method converts a number to a string of its fixed-point decimal form. ... Front-end engineers work closely with designers to make websites beautiful, functional, and fast.