Java Script : 'toFixed' Math function for number format

Jun 30, 2009 Posted by Lara Kannan
<div style="text-align: justify;">We can use 'toFixed()' function in math to format a number upto required decimal places. The value we get is after rounding the number upto the decimal place required. This function is frequently used in java script. </div>

Here is the syntax.
my_val.toFixed(4)

my_val is the variable which stores the number. Here we are formatting the number upto 4 decimal places.

Let us try with some examples with different number values.
var my_val=11.257;
document.write (my_val.toFixed(2)); // output 11.26

var my_val=11.25;
document.write (my_val.toFixed(1)); // output 11.3

var my_val=11.978;
document.write (my_val.toFixed(4)); // output 11.9780

Use the tips!
Share
Labels:

Post a Comment