Possible Duplicate:
Retain precision with Doubles in java
import static java.lang.System.out;
public class q2{
public static void main(String args[]){
double x=4.02, y=0.05;
out.println(x+y);
}
}
Output:
4.069999999999999
Why is it outputting the that. I thought it would be 4.07. Please explain why this happens in java ?
Sorry for the inaccurate Question title. I can’t have a better title than this
That is because some numbers — such as 0.1 — cannot be represented exactly in binary floating-point.
Consider reading the following article:
Arithmetic