Math.round()
rounds to the nearest integer. To round to any other digit, divide and multiply by powers of ten.
One such method is this:
function round(num,pre) {
if( !pre) pre = 0;
var pow = Math.pow(10,pre);
return Math.round(num*pow)/pow;
}
You can make similar functions for floor
and ceiling
. However, no matter what you do, 10
will never round to 20
.