In java:
double b = 1234 / (1234+1500);
result is:
0.0
why?
How to get correct result?
double b = 1234D / (1234D + 1500D)
just make one of the operand double
/float
-
double b = (double) 1234.0/(1235+1500);
Here casting is not required.
The rules benind: if one of the operand is double
/float
(here 1234.0) the other is promoted to double
/float
.
You get 0.0
because java will be an integer devision. To get the correct result you have to cast at least on operator to double
.
double b = ((double)1234) / (1234+1500)