SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Math Round function in JavaScript

We can round off any number with decimal place to an integer value by using round function. If the decimal portion of the number is equal or greater than .5 then the next higher integer is returned or if the number is less than 05 then the number is rounded to next lowest integer.

Here is the syntax.


Math.round(number)



Here are some examples.



document.write (Math.round(2.65));// print 3
document.write (Math.round(7.05));// print 7
document.write (Math.round(-2.65));// print -3
document.write (Math.round(-8.15));// print -8
document.write (Math.round(11.65));// print 12


We can format math number by using round function. Here is an example to format upto two decimal places.
var my_val=11.257;
var my_val=11.254;

document.write (Math.round(my_val*100)/100);


But we can use math toFixed function to format any number

You can read the php math round function
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
Loops & structure
JavaScript Forum
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.