This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Advice on BigDecimal vs. double Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Advice on BigDecimal vs. double" Watch "Advice on BigDecimal vs. double" New topic
Author

Advice on BigDecimal vs. double

Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
howdy
I'm in the progress of modeling a system for a financial company and I was wondering wether I should use BigDecimal or double for financial data.
From what I've read by now you can't really trust double to store financial numbers but BigDecimal has some downsided too:
- it's slow (MUCH slower)
- can't use normal operators on them; have to resort to method calls
- only basic operations on BigDecimals are available

One possible improvement over BigDecimal would be to use IBM's BigDecimal class ( http://www2.hursley.ibm.com/decimalj/ ). It's 'compatible' with java.math.BigDecimal but IBM has added aditional methods and functionality + they say it's 23 times faster. (btw: JSR13 )
So; should I use double ? or BigDecimal ? or IBM's BigDecimal ? what would YOU do ?

any advice on when to use BigDecimal would be welcome too
thanks
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
Calculations with BigDecimal are slower as compared to those with double but if accuracy and precision are important than you should go for BigDecimal rather than double.


Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Why not just use ints? Multiply all your numbers by 100 so that you carry cents. Then just worry about converting them to a decimal format when you need to store or display them.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Thomas Paul:
Why not just use ints? Multiply all your numbers by 100 so that you carry cents. Then just worry about converting them to a decimal format when you need to store or display them.

Well, afaik most financial applications need to work on the thousandth part of a cent or something, but you should be able to use int or long as long as you don't need *arbitrary* precision. (Don't forget to encapsulate it in its own Money class!)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
thanks so far everybody
I'm not so keen on implementing my own 'Money' class The thing is I'm using these things with EJB and I'm wondering wether it'll all work...
Perhaps the BigDecimal isn't so bad after all. I know that works for shure...
Pitty java doesn't have operator overloading I remember stuff back from the C++ days, it can really help you out in some situations. I do understand SUNs decision not to implement operator overloading though..
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
What do you think could be accomplished with operator overloading that cannot be otherwise accomplished?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Well, you could write things like

where all variables are BigDecimal. Instead we have to use things like

If you're using lots of formulas, operator overloading can give you much better readability. Of course it can also obfuscate the heck out of other examples.


"I'm not back." - Bill Harding, Twister
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
C# has operator overloading.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Advice on BigDecimal vs. double
 
Similar Threads
BigDecimal and the lack of information about rounding
JTable with labels and buttons ?
java decimal multiplication
What is OSGI?
java modulus operator