shin shin - 1 year ago 124
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;

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download