Math.Round in javascript
by Sanju[ Edit ] 2009-11-01 00:00:15
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....