File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes double addition error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double addition error" Watch "double addition error" New topic

double addition error

Deli Dumrul

Joined: Jul 06, 2004
Posts: 9

Sorry if this problem was asked before

When i add 0.14+0.01 it gives 0.15000000000000002.

Looks like one way to get rid of this problem is to use BigDecimal.

Is there another way and how do some other compilers like visual c++ handle this that they don't give this error for double values?

Greg Charles

Joined: Oct 01, 2001
Posts: 2968

Rounding errors are a fact of life when doing floating point arithmetic on a computer. The compiler has nothing to do with it, because it's caused by the inner workings of the CPU. What you can do is round the result (see Math.round) or use a number formatter when displaying it.
I agree. Here's the link:
subject: double addition error
It's not a secret anymore!