SQL PHP HTML ASP JavaScript articles and free scripts to download
 

toFixed Math function to format 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.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
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>");



You can read the php math number format
Further readings
random() function to generate random numbers
round() function to get rounded value of a number
IsNaN() function to check data if number or not
ceil() function to get just next higher integer
floor() function to get just highest lower integer
toFixed() function to format decimal places of a number
toPrecision() function to fix the places of a number
parseFloat() function to convert string to number
 
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
JavaScript Tutorials
Math Functions
Popular Tutorials
Drop down list
Timer function
JavaScript Tutorials
String
Array
Date & Time
Form Validation
Event Handling
Math Functions
JavaScript Forum
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.