views:

2472

answers:

11

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?

+17  A: 

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.

ninesided
+1. We've decided to add a container class that consumes a currency, too. This comes in handy when rendering monetary values in tables.
dhiller
yep, that's a pretty common approach and it makes a lot of sense. One caveat to this is when you have to deal with Japanese Yen, as they don't have a minor currency denomination like cents, so it needs it's own rounding rules.
ninesided
+3  A: 

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.

Ken Gentle
Strictly speaking, BigDecimal is also inexact; it just corresponds better with the decimal rounding we're used to in daily life, and allows you to specify rounding modes.
Michael Borgwardt
+3  A: 

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.

Bill K
A: 

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!

Pyrolistical
Why would you need to be "forced" to use BigDecimal? What are you unsure about? It's clearly superior to working with cents, as it allows you to explicitly specify rounding modes.
Michael Borgwardt
+1  A: 

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.

Kozyarchuk
+2  A: 

There is a better library, it`s timeandmoney. IMO, it far superior to the libraries provided by the JDK for representing these 2 concepts.

Don
A: 

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.

lindelof
and the underlying data type of his Money class? BigDecimal.
ninesided
A: 

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

dshaw
A: 

You can use the DecimalFormat class when ultimately displaying a currency value. It provides localization support and is pretty extensible.

A: 

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"));
Per Arneng
+2  A: 

It can be useful to people arriving here by search engines to know about JodaMoney: http://joda-money.sourceforge.net/.

Thanks. I've been meaning to add a follow-up note about Joda Money. Have you used it?
dshaw