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

BigDecimal vs double

Denis Wen
Ranch Hand

Joined: Nov 11, 2008
Posts: 33
His,

A question of interest. I keep reading code of sample applications (some from respectable sources) that use type double to represent monetary values. On the other hand, not once did I come across comments stating evils of double. Could maybe someone outline benefits of BigDecimal?

Thanks
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
Java cannot represent floating point precisely.
Try doing 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1 ===> This will give 0.9999999999999999 and not 1.0

So, to avoid any precision loss use BigDecimal. For any processing related values, use BigDecimal


Sunil Kumar
http://goodtoknowit.blogspot.com/
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
Adding to this,
Do not use the constructor for BigDecimal that uses double. This will anyhow remove the precision. Use String constructor instead
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
The books vary on that question.
Deitel and Deitel have a little inset saying to use BigDecimal or integer arithmetic.
Barry Burd's book (at least the one I have read) seems happy to use floating-point. Even though he presents an example where there is a millionth-of-a-cent error.
Joshua Bloch (Effective Java) and, even more so, Bloch and Gafter (Java Puzzlers) have examples in where floating-point arithmetic wll let you down badly.
But Sunil Kumar is spot on.
Denis Wen
Ranch Hand

Joined: Nov 11, 2008
Posts: 33
Thanks for clarifications Is that that behavior of BigDecimal with a double constructor a known bug or expected behavior though?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Denis, you don't have to ask that here. All you need to do is to read the API documentation for that constructor, which like the rest of the Java API is freely available online for your use.
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
Denis, for double based constructor , the value will be stored in double variable and hence a precision loss again.
I dont think it would be a bug, as it also relates to the floating point behavior
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

As Campbell mentioned, Effective Java by Joshua Bloch has a very nice, succinct, and compelling explanation of why BigDecimal should be used rather than doubles, especially for monetary calculations. The item is named Avoid float and double if exact answers are required and is Item #31 in the first edition (ISBN-10: 0-201-31005-8) and Item #48 in the second edition (ISBN-10: 0-321-35668-3). It has some simple monetary calculation examples that show how easily and quickly doubles can corrupt results. It'd be well worth it to find a copy of this book in a library to read this information. Although in my humble opinion, all Java Developers should have a copy of this book and should read it annually.

If you want to investigate this subject in detail, the book Java� Number Cruncher: The Java Programmer's Guide to Numerical Computing by Ronald Mak (ISBN-10: 0-321-35668-3) covers the subject nicely. Given the complexity of the subject matter, it is an easy read. Although the last few chapters do get mathematically intense.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BigDecimal vs double