- JavaScript Number toFixed() method
- JavaScript: Number toFixed() method
- Syntax:
- Here,
- List of Examples of Number toFixed Methods
- Source code of this Example
- Demo of toFixed() javaScript
- Note
- How can I round a number in JavaScript? .toFixed() returns a string?
- Javascript toFixed With and Without Rounding
- Demo of Javascript toFixed With and Without Rounding
- Let’s see more examples javascript tofixed method
- Conclusion
- Number.prototype.toFixed()
- Try it
- Syntax
- Parameters
- Return value
- Exceptions
- Description
- Examples
- Using toFixed()
- Using toFixed() with negative numbers
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- JavaScript Number toFixed()
- Note
- Syntax
- Parameters
- Return Value
- More Examples
- Browser Support
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- JavaScript: Number toFixed() method
- Description
- Syntax
- Parameters or Arguments
- Returns
- Note
- Example
- Specifying an Exponential Notation
- Padding the Decimal Places
JavaScript Number toFixed() method
JavaScript: Number toFixed() method; In this tutorial, You will learn how to use the number method called toFixed with new examples. And as well as learn how to number tofixed without rounding in javaScript.
JavaScript: Number toFixed() method
The JavaScript toFixed () method is used to round( formats ) a number to a fixed notation point or convert the number into a string and its representation of a number with a specified number of decimal points. It returns value as a string.
Syntax:
Here,
- The first thing is n is an optional parameter in toFixed function.
- The second thing is, n is a number of digits after the decimal place to display in the result.
List of Examples of Number toFixed Methods
Let’s take a look at an example javascript number format one decimal point, two decimal points or without decimal using the toFixed() method in JavaScript.
var number = 111.123456; number.toFixed(); number.toFixed(1); number.toFixed(2); //Output 111 111.1 111.12
Source code of this Example
Demo of toFixed() javaScript
JavaScript toFixed() Method Example Click & See Result
Note
Assuming you have a decimal number greater than the real number, taps are added to make the desired decimal length.
How can I round a number in JavaScript? .toFixed() returns a string?
Javascript toFixed With and Without Rounding
Let’s take a new example for rounding a number with or without using .toFixed function.
With Round :- )?/)[0]; document.getElementById("withRound").innerHTML = withRound; document.getElementById("roundValue").innerHTML = with2Decimals; >
Demo of Javascript toFixed With and Without Rounding
Javascript tofixed With and Wthout Rounding Example Click & See Result
Let’s see more examples javascript tofixed method
var num = 12345.6789; num.toFixed(); // Returns '12346': note rounding, no fractional part num.toFixed(1); // Returns '12345.7': note rounding num.toFixed(6); // Returns '12345.678900': note added zeros (1.23e+20).toFixed(2); // Returns '123000000000000000000.00' (1.23e-10).toFixed(2); // Returns '0.00' 2.34.toFixed(1); // Returns '2.3' 2.35.toFixed(1); // Returns '2.4'. Note it rounds up 2.55.toFixed(1); // Returns '2.5'. Note it rounds down - see warning above -2.34.toFixed(1); // Returns -2.3 (due to operator precedence, negative number literals don't return a string. ) (-2.34).toFixed(1); // Returns '-2.3'
Conclusion
In this tutorial, you have learned how to use javascript .tofixed function with examples
If you have any questions or thoughts to share, use the comment form below to reach us.
Number.prototype.toFixed()
The toFixed() method formats a number using fixed-point notation.
Try it
Syntax
Parameters
The number of digits to appear after the decimal point; should be a value between 0 and 100 , inclusive. If this argument is omitted, it is treated as 0 .
Return value
A string representing the given number using fixed-point notation.
Exceptions
Thrown if digits is not between 0 and 100 (inclusive).
Thrown if this method is invoked on an object that is not a Number .
Description
The toFixed() method returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.
If the absolute value of numObj is greater or equal to 10 21 , this method uses the same algorithm as Number.prototype.toString() and returns a string in exponential notation. toFixed() returns «Infinity» , «NaN» , or «-Infinity» if the value of numObj is non-finite.
The output of toFixed() may be more precise than toString() for some values, because toString() only prints enough significant digits to distinguish the number from adjacent number values. For example:
(1000000000000000128).toString(); // '1000000000000000100' (1000000000000000128).toFixed(0); // '1000000000000000128'
However, choosing a digits precision that’s too high can return unexpected results, because decimal fractional numbers cannot be represented precisely in floating point. For example:
(0.3).toFixed(50); // '0.29999999999999998889776975374843459576368331909180'
Examples
Using toFixed()
const numObj = 12345.6789; numObj.toFixed(); // '12346'; rounding, no fractional part numObj.toFixed(1); // '12345.7'; it rounds up numObj.toFixed(6); // '12345.678900'; additional zeros (1.23e20).toFixed(2); // '123000000000000000000.00' (1.23e-10).toFixed(2); // '0.00' (2.34).toFixed(1); // '2.3' (2.35).toFixed(1); // '2.4'; it rounds up (2.55).toFixed(1); // '2.5' // it rounds down as it can't be represented exactly by a float and the // closest representable float is lower (2.449999999999999999).toFixed(1); // '2.5' // it rounds up as it's less than NUMBER.EPSILON away from 2.45. // This literal actually encodes the same number value as 2.45 (6.02 * 10 ** 23).toFixed(50); // 6.019999999999999e+23; large numbers still use exponential notation
Using toFixed() with negative numbers
Because member access has higher precedence than unary minus, you need to group the negative number expression to get a string.
-2.34.toFixed(1); // -2.3, a number (-2.34).toFixed(1); // '-2.3'
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 26, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
JavaScript Number toFixed()
The toFixed() method rounds the string to a specified number of decimals.
Note
If the number of decimals are higher than in the number, zeros are added.
Syntax
Parameters
Return Value
More Examples
Browser Support
toFixed() is an ECMAScript3 (ES3) feature.
ES3 (JavaScript 1999) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
JavaScript: Number toFixed() method
This JavaScript tutorial explains how to use the Number method called toFixed() with syntax and examples.
Description
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. Because toFixed() is a method of the Number object, it must be invoked through a particular instance of the Number class.
Syntax
In JavaScript, the syntax for the toFixed() method is:
number.toFixed([decimalPlaces]);
Parameters or Arguments
decimalPlaces Optional. It is the number of digits after the decimal place to display in the result. If this parameter is omitted, the decimalPlaces will default to 0.
Returns
The toFixed() method converts a number to fixed-point notation with the indicated number of decimalPlaces (rounding the result where necessary) and then returns its value as a string.
If more decimal places are required than was present in the original number, the toFixed() method will pad the result with 0’s after the decimal place.
Note
- The toFixed() method will round the resulting value if necessary.
- The toFixed() method will pad the resulting value with 0’s if there are not enough decimal places in the original number.
- The toFixed() method does not change the value of the original number.
Example
Let’s take a look at an example of how to use the toFixed() method in JavaScript.
var totn_number = 123.456789; console.log(totn_number.toFixed()); console.log(totn_number.toFixed(1)); console.log(totn_number.toFixed(2));
In this example, we have invoked the toFixed() method using the Number class.
We have written the output of the toFixed() method to the web browser console log, for demonstration purposes, to show what the toFixed() method returns.
The following will be output to the web browser console log:
In this example, the first output to the console log returned the string value «123» which is the fixed-point notation for 123.456789 rounded to 0 decimal places.
The second output to the console log returned the string value «123.5» which is the fixed-point notation for 123.456789 rounded to 1 decimal place.
The third output to the console log returned the string value «123.46» which is the fixed-point notation for 123.456789 rounded to 2 decimal places.
Specifying an Exponential Notation
The toFixed() method can also handle converting exponential notation values to fixed-point notation.
So if we rewrote our numeric value of 123.456789 as the equivalent exponential value (123.456789 = 1.23456789e+2), we could change our example as follows:
var totn_number = 1.23456789e+2; console.log(totn_number.toFixed()); console.log(totn_number.toFixed(1)); console.log(totn_number.toFixed(2));
The following will be output to the web browser console log:
In this example, the first output to the console log returned the string value «123» which is the fixed-point notation for 1.23456789e+2 rounded to 0 decimal places.
The second output to the console log returned the string value «123.5» which is the fixed-point notation for 1.23456789e+2 rounded to 1 decimal place.
The third output to the console log returned the string value «123.46» which is the fixed-point notation for 1.23456789e+2 rounded to 2 decimal places.
Padding the Decimal Places
Finally, let’s explore how the toFixed() method pads the result with 0’s if there are not enough decimal places in the original number.
var totn_number = 123.45; console.log(totn_number.toFixed(3)); console.log(totn_number.toFixed(4));
The following will be output to the web browser console log:
In this example, the first output to the console log returned the string value «123.450» which is the fixed-point notation for 123.45 padded to 3 decimal places with 0’s.
The second output to the console log returned the string value «123.4500» which is the fixed-point notation for 123.45 padded to 4 decimal places with 0’s.