Michal Žídek Michal Žídek - 3 years ago 107
Java Question

How do I ceil number (etc: 5 to 10)

I have a problem with

Math.ceil
in Java, because it ceils and floors only decimal numbers. I need a function which can ceil or floor any number including an integer. For example if I have 5, I would want it to become 10 or if I have, for example 2, I would want it to become 0.

I am pretty sure that some easy function exists for this, but I could not find it. I would be really grateful for some examples on how to do that.

Answer Source
public class CustomCeiling {

    public static double getCeiling(double number){
        return Math.round(number/10)*10;
    }

    public static void main(String args[]){
        System.out.println(getCeiling(2));
        System.out.println(getCeiling(4));
        System.out.println(getCeiling(5));
        System.out.println(getCeiling(10));
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download