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

addition accuracy

Jason Hocker
Ranch Hand

Joined: Jul 23, 2003
Posts: 132
Why does this return the value 55296.19000000006? How can I get 55296.19?

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Floating point numbers usually don't have exact representations. When you print out a double, you can use the DecimalFormat class.
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
I think the only case where you're guaranteed 100% accuracy is when the fractional parts of the values involved can be represented by sums of negative powers of 2.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42951
Point #20 in the JavaBeginnersFaq covers this.
I agree. Here's the link:
subject: addition accuracy