|
|
Math.Round in javascript - Javascript
|
Views : 545
|
|
Tagged in : Javascript
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
Math.round(x) function can be used to round a number to nearest integer value
For example
Math.round(15.9) //returns 16
Math.round(2.1) //returns 2
Math.round(-1.5 //returns -2
To round it to decimal places we have to use multiplication and division.
So if we want to round to 2 decimal places. Multiply the number by 100 and then call Math.Round on that number and later divide it by 100.
Example:
Round a number 12.121212 to 2 decimal places
- Math.Round(12.121212*100)/100; //Returns 12.12
if we want 3 decimal places just use 1000 instead of 100 like
- Math.Round(12.121212*1000)/1000; //Returns 12.121
and so on....
|
|
By Sanju, On - 2009-11-01 |
|
|
|