Math Round Function of Javascript returns the rounded integer value nearest to a number passed to the Math.round(x) function. Math round function rounds off the precision point values, if the last decimal point value is greater than or equal to 5 then it increases the value of previous precision place value by 1 and so on to first precision point value. At last Math.round function checks that if the decimal point value is greater than or equal to 5 then it returns then integer by adding 1 to the decimal number at the left side of the decimal point. Math.round function starts this round off process from extreme right precision point value to the left most values. If precision point value is less than 5 then the point value at its previous place remains unchanged.
Math.round(x);
Math.round function accepts the value as number and returns the rounded integer value for it.
Place this example code in your HTML page inside the HTML body or Head Section of the HTML document:
<script language="javascript" type="text/javascript">
document.write(Math.round(0.51) + "<br />");
document.write(Math.round(0.44) + "<br />");
document.write(Math.round(2.44) + "<br />");
document.write(Math.round(-0.44) + "<br />");
document.write(Math.round(-0.51) + "<br />");
document.write(Math.round(-2.44) + "<br />");
</script>
Output:
1 0 2 0 -1 -2
Be the first to rate this post
Tags: javascript, javascript math.round, javascript math functions, javascript math object, javascript math properties, javascript math.abs, javascript math.ceil, javascript math.floor, javascript math.pow, javascript math.log, javascript math round function, javascript math.sqrt, javascript math.random
10/11/2008 3:42:04 AM