shin shin - 1 month ago 5
Java Question

Mistake while adding double values in java

I have the following code:

double d1=1000000000000000000.0; //which is equivalent to 1.0e^18
double d2=3434.0;
System.out.println(d1+d2);


which prints 1000000000000003460 instead of 1000000000000003434. What is the problem?

Answer

@Anthony Pegram is right. If you need such precision use BigDecimal.