Why does (360 / 24) / 60 = 0 … in Java

None of the operands in the arithmetic is a float – so it’s all being done with integer arithmetic and then converted to a float. If you change the type of an appropriate operand to a float, it’ll work fine:

float div = ((360 / 24f) / 60); // div is now 0.25

Note that if you changed just 60 to be a float, you’d end up with the 360 / 24 being performed as integer arithmetic – which is fine in this particular case, but doesn’t represent what I suspect you really intended. Basically you need to make sure that arithmetic operation is being performed in the way that you want.

Leave a Comment