If all operand of a division are int
, then you’ll do int division
and it would provide only an int value (or an int rounded value if you store in double)
At least one of the operand should be double
double h = 1.0 / 4;
System.out.println(h); // 0.25
h = 1d / 4;
System.out.println(h); // 0.25
h = 1 / 4.0;
System.out.println(h); // 0.25
h = ((double) 1) / 4;
System.out.println(h); // 0.25
solved Java: Dividing doesn’t work the way it used to [duplicate]