Friday, 2 March 2012

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);

No comments:

Post a Comment