Possible duplicate:Strange floating-point behavior in a Java program.Why splitting JSP / JSTL into 1000 sometimes gives a remainder?
I am trying to get numbers after a decimal number. ex: 60.4 →0.4
60.
0.4
However, when do
double a = 60.4 % 1;
it looks like 0.3999999999999986.
0.3999999999999986
Why is this? And how can this be fixed?
Use fixed point types
BigDecimal src = new BigDecimal("60.4"); BigDecimal a = src.remainder(BigDecimal.ONE);
You can use DecimalFormat to accomplish the desired task.
, : ? (Java)
I think this is exactly what you are looking for. So you can use:
double x = d - Math.floor(d);
OR
BigDecimal class for exact digits after the decimal number.