Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Number toFixed() Method

Improve Article
Save Article
  • Last Updated : 01 Dec, 2022
Improve Article
Save Article

The JavaScript Number toFixed( ) method in JavaScript is used to format a number using fixed-point notation. It can be used to format a number with a specific number of digits to the right of the decimal.

Syntax:

number.toFixed( value )

The toFixed() method is used with a number as shown in the above syntax using the ‘.’ operator. This method will format a number using fixed-point notation.

Parameters: This method accepts a single parameter value. It signifies the number of digits to appear after the decimal point.

Return Value: It returns a number in the string representation. The number has the exact number of digits after the decimal place as mentioned in the toFixed() method. 

Below is an example of the Number toFixed( ) Method.

Example 1: Using toFixed() method with a parameter 1, displays decimal points to one digit.

Javascript




<script type="text/javascript">
    var test=213.73145;
    console.log(test.toFixed(1));        
</script>


Output:

213.7

Example 2:Using toFixed() method without any parameter, If there is no parameter specified in the toFixed() method then it doesn’t display any digits after the decimal place. 

Javascript




<script type="text/javascript">
    var test=213.73145;
    console.log(test.toFixed());        
</script>


Output:

214

Example 3: Using toFixed() method with a parameter, If there is a parameter specified in the toFixed() method will return a number represented as a string which will have exactly that number of digits after the decimal place. 

html




<script type="text/javascript">
    var test=213.73145;
    console.log(test.toFixed(3));        
</script>


Output:

214.731

Example 4: Using toFixed() method where the number is in exponential form. The toFixed() method can be used to convert an exponential number into a string representation with a specific number of digits after the decimal place. 

Javascript




<script type="text/javascript">
    var test=2.13e+15;
    console.log(test.toFixed(2));        
</script>


Output:

2130000000000000.00

We have a complete list of Javascript Number Objects methods, to check those please go through this Javascript Number Complete reference article.

Supported Browsers:

  • Google Chrome 1 and above
  • Internet Explorer 5.5 and above
  • Microsoft Edge 12 and above
  • Firefox 1 and above
  • Apple Safari 2 and above
  • Opera 7 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!