Javascript Math Round Function

by top54u.com 21 Jul, 2008

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.

 

Syntax for Javascript Math Round Function

 

Math.round(x);

Math.round function accepts the value as number and returns the rounded integer value for it.

 

Examples of Javascript Math Round Function

 

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

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:42:04 AM




related videos.....
recent posts.....
top54u ezines.....