toFixed Math function to format ( decimal places ) number

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 to format currency, marks etc.

Here is the syntax.

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
document.write ("<br>----<br>");

var my_val=11.25;
document.write (my_val.toFixed(1)); // output 11.3 q 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.

Demo of toFixed()

By using toFixed function we can manage the length of decimal places but to manage total number including decimal places we have to use Pricision function.

You can read the php math number format

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Your Rating


Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2016 All rights reserved worldwide Privacy Policy Disclaimer