Pages

Javascript Number - toFixed()

4/4/14

Description:

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

Syntax:

number.toFixed( [digits] )
Here is the detail of parameters:
  • 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 exactly digitsdigits after the decimal place.

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>
This will produce following result:
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 

No comments:

Post a Comment

Related Posts