user6264026 user6264026 - 8 months ago 18
Java Question

Why does 1 / 2 == 0 using double?

I'm a high school student currently getting ready for a state academic meet(UIL). I have a problem and I've looked everywhere and can't seem to find an answer! Why does this print out 0.0?

double d = 1/2;


It's because of the data type.

When you do 1/2 that is integer division because two operands are integers, hence it resolves to zero (0.5 rounded down to zero).

If you convert any one of them to double, you'll get a double result.

double d = 1d/2;


double d = 1/2.0;