Java: round to nearest multiple of 5 (either up or down)

Nearest Multiple of 5 for Upper value

5*(Math.ceil(Math.abs(number/5)));

for Lower Value

5*(Math.floor(Math.abs(number/5)));

it gives Positive value only.


haven't tested it, but 5*(Math.round(f/5)); should work


public static void main(String args[]) {
    double num = 67.5;
    if (num % 5 == 0)
        System.out.println("OK");
    else if (num % 5 < 2.5)
        num = num - num % 5;
    else
        num = num + (5 - num % 5);
    System.out.println(num);

}

Try this.

Tags:

Java

Rounding