Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal vs double

 
Denis Wen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarifications Is that that behavior of BigDecimal with a double constructor a known bug or expected behavior though?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic