The problem here is, that the multiplication 5.1d * 100d
produces a rounding error, that results in 509.99999999999994d
. And when you cast this double into an integer, then the result is obviously 509
.
To understand what is going on here, consider the following:
double doubleValue = 5.1d * 100d; // rounding error...
System.out.println("Double: " + doubleValue);
int intValue = (int) doubleValue;
System.out.println("Integer: " + intValue);
Output:
Double: 509.99999999999994
Integer: 509