nschmeller
nschmeller

Reputation: 79

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?

Upvotes: 0

Views: 259

Answers (2)

Henry
Henry

Reputation: 43798

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

Upvotes: 2

Brandon Schabell
Brandon Schabell

Reputation: 1925

This should be logically equivalent to subtracting one and then taking the ceiling of the result.

Upvotes: 0

Related Questions