Search code examples
javaintroundingdivide

Java round down including the next integer


I'm curious about whether Java has a capability to round down from the next integer inclusive, e.g. 4 rounds down to 3, or 10 rounds down to 9.

For some context, I'm doing int divides, so my result will already be rounded down. 6/4 would round to 1, 7/4 would round to 1, but I want 8/4 which naturally rounds to 2 to instead round to 1. 9/4 would then round to 2, continuing to 12/4 rounding to 2.

Is there a function or code trick to achieve this?


Solution

  • This is easy to achieve if you do a division anyhow. Just do (a-1)/b as integer division.