var my_val=56.879654
document.write(my_val.toFixed(4)); // Output 56.8797
my_val is the variable which stores the number. Here we are formatting the number upto 4 decimal places.
var my_val=11.257;
document.write (my_val.toFixed(2)); // output 11.26
document.write ("<br>----<br>");
var my_val=11.25;
document.write (my_val.toFixed(1)); // output 11.3
document.write ("<br>----<br>");
var my_val=11.978;
document.write (my_val.toFixed(4)); // output 11.9780
document.write ("<br>----<br>");
While using any data coming from a text box or any other sources, we have to use parseInt or parseFloat functions to convert the data from string to integer or float before using toFixed function.
We can check different values by using a demo script which returns data after executing toFixed function from the user entered data. By using toFixed function we can manage the length of decimal places but to manage total number including decimal places we have to use toPrecision() function.