I see that you have tried to force a floating-point result by storing the result in a float
, but unfortunately this won't do it. Operators in Java, such as the division operator (/
) work like this:
- If BOTH operands are floats, the result is a float.
- If ONE of the operands is a float, the result is a float.
- If NEITHER of the operands are floats (i.e. they are both integers), the result is an
int
.
Since yours falls into the latter case, you get an integer result. The solution is to force one of your operands to be a float, not just the resulting variable, like so:
ires = ((float) in1) / in2;