toFixed Math function to format ( decimal places ) number



Enter a number

Note: The decimal part is kept to 2 digits using toFixed(2) function. For any length of value the decimal places will remain 2 places only. ( Try by entering different length of decimal data )

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.


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










    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