I understand that BigDecimal is recommended best practice for representing monetary values in Java. What do you use? Is there a better library that you prefer to use instead?
BigDecimal
all the way. I've heard of some folks creating their own Cash
or Money
classes which encapsulate a cash value with the currency, but under the skin it's still a BigDecimal
, probably with BigDecimal.ROUND_HALF_EVEN
rounding.
Edit: As Don mentions in his answer, there are open sourced projects like timeandmoney, and whilst I applaud them for trying to prevent developers from having to reinvent the wheel, I just don't have enough confidence in a pre-alpha library to use it in a production environment. Besides, if you dig around under the hood, you'll see they use BigDecimal
too.
BigDecimal
or another fixed point representation is what is generally needed for money.
Floating point (Double
, Float
) representations and calculations are inexact, leading to erroneous results.
If you are just using dollars and cents, I'd use a long (offset by 2 decimal places). If you need more detail, big decimal may be the way to go.
Either way, I'd probably extend the class to have a .toString() that uses the correct format, and as a place to put other methods that might come up (For a long, multiplying and dividing will go awry if the decimal isn't adjusted)
Also, if you use define your own class and interface, then you can replace the implementation at will.
You have to be so careful when dealing with time and money.
When you are working with money, I hope everybody should know never to use a float or a double.
But I am unsure about BigDecimal.
In most cases you'll be fine if you just keep track of cents in a int or long. This way you never deal with a decimal place.
You only display dollars when you print it. Always work with cents internal using integers. This may be tricky if need to divide or need to use Math.abs().
However, you might care able half a cent, or even one hundredth of a cent. I don't know what's a good way to do this. You might just need to deal with thousandth of cents and use a long. Or maybe you'll be forced to use BigDecimal
I would do a lot more reading on this, but ignore everybody who starts talking about using a float or double to represent money. They are just asking for trouble.
I feel my advice isn't complete, so please put more though into it. You are dealing with dangerous types!
Creating a Money class is the way to go. Using BigDecimal( or even an int) underneath. Then using Currency class to define rounding convention.
Unfortunately without operator overloading Java makes it quite unpleasant creating such basic types.
There is a better library, it`s timeandmoney. IMO, it far superior to the libraries provided by the JDK for representing these 2 concepts.
Definitely not BigDecimal. There are so many special rules for rounding and presentation that you have to worry about.
Martin Fowler recommends the implementation of a dedicated Money class to represent currency amounts, and that also implements rules for currency conversion.
Thanks guys for the useful and interesting discussion. I selected ninesided's answer because I think it provides the best condensation of the discussion for future reference. However, if it were possible to select two, I would have selected Don's Answer since alternative libraries that were not on my radar was impetus behind this question. Thanks Don. Cheers everybody. - Daniel
I would encapsulate BigDecimal in Money class that also has a currency just as someone mentioned above. The important thing is that you do an extreme amount of unit tests and especially if working with different currencies. Also it is a good idea if you add a convinient constructor that takes a string or a factory method that does the same so that you can write your tests something like this:
assertEquals(Money.create("100.0 USD").add("10 GBP"),Money.create("116 USD"));
It can be useful to people arriving here by search engines to know about JodaMoney: http://joda-money.sourceforge.net/.