How to round up to the next integer?

No, Math.ceil() won’t work on its own because the problem occurs earlier. a and b are both integers, so dividing them evaluates to an integer which is the floor of the actual result of the division. For a = 3 and b = 2, the result is 1. The ceiling of one is also one – hence you won’t get the desired result.

You must fix your division first. By casting one of the operands to a floating point number, you get a non-integer result as desired. Then you can use Math.ceil to round it up. This code should work:

Math.ceil((double)a / b);

Leave a Comment