Hay, how would i go about rounded a number up the nearest multiple of 3?
ie
25 would return 27
1 would return 3
0 would return 3
6 would return 6
thanks
Hay, how would i go about rounded a number up the nearest multiple of 3?
ie
25 would return 27
1 would return 3
0 would return 3
6 would return 6
thanks
if(n > 0)
return Math.ceil(n/3.0) * 3;
else if( n < 0)
return Math.floor(n/3.0) * 3;
else
return 3;
Here you are!
Number.prototype.roundTo = function(num) {
var resto = this%num;
if (resto <= (num/2)) {
return this-resto;
} else {
return this+num-resto;
}
}
Examples:
y = 236.32;
x = y.roundTo(10);
// results in x = 240
y = 236.32;
x = y.roundTo(5);
// results in x = 235