JavaScript Number - toFixed()


Advertisements


Description

This method formats a number with a specific number of digits to the right of the decimal.

Syntax

Its syntax is as follows −

number.toFixed( [digits] )

Parameter Details

digits − The number of digits to appear after the decimal point.

Return Value

A string representation of number that does not use exponential notation and has the exact number of digits after the decimal place.

Example

Try the following example.

<html>
   <head>
      <title>JavaScript toFixed() Method</title>
   </head>
   <body>
      
      <script type="text/javascript">
         var num=177.1234;
         document.write("num.toFixed() is : " + num.toFixed()); 
         document.write("<br />"); 
      
         document.write("num.toFixed(6) is : " + num.toFixed(6)); 
         document.write("<br />"); 
      
         document.write("num.toFixed(1) is : " + num.toFixed(1)); 
         document.write("<br />"); 
      
         document.write("(1.23e+20).toFixed(2) is:" + (1.23e+20).toFixed(2)); 
         document.write("<br />"); 
      
         document.write("(1.23e-10).toFixed(2) is : " + (1.23e-10).toFixed(2)); 
      </script>
   
   </body>
</html>

Output

num.toFixed() is : 177
num.toFixed(6) is : 177.123400
num.toFixed(1) is : 177.1
(1.23e+20).toFixed(2) is:123000000000000000000.00
(1.23e-10).toFixed(2) is : 0.00 

javascript_number_object.htm

Advertisements