close
The Wayback Machine - https://web.archive.org/web/20220812135615/https://www.geeksforgeeks.org/javascript-tofixed-function/amp/

JavaScript Number toFixed( ) Method

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




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

213.7

The 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 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. 

Example




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

Output:

214




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

Output:

214.731




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

Output:

2130000000000000.00

Supported Browsers:




Article Tags :