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.

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.

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 
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 toPrecision() function.

You can read the php math number format
Format a decimal number to number of places
JavaScript Math Reference
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer