Please keep in mind I only want to round DOWN to the nearest multiple of 20, never up.
Thanks a lot!
One solution would be to subtract the results of modulo 20 (which is the remainder from division by 20) from the initial value. Something like,
double[] in = { 22, 45, 69.5, 60 };
for (double d : in) {
int v = (int) d;
v -= v % 20;
System.out.printf("%.1f --> %d%n", d, v);
}
Output is
22.0 --> 20
45.0 --> 40
69.5 --> 60
60.0 --> 60
public static Integer round20(Integer b){
return b - (b%20);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With