Rounding up a number to nearest multiple of 5
To round to the nearest of any value
int round(double i, int v){
return Math.round(i/v) * v;
}
You can also replace Math.round()
with either Math.floor()
or Math.ceil()
to make it always round down or always round up.
int roundUp(int n) {
return (n + 4) / 5 * 5;
}
Note - YankeeWhiskey's answer is rounding to the closest multiple, this is rounding up. Needs a modification if you need it to work for negative numbers. Note that integer division followed by integer multiplication of the same number is the way to round down.