I know the problem with double/float, and it's recommended to use BigDecimal instead of double/float to represent monetary fields. But double/float is more effective and space-saving. Then my question is: It's acceptable to use double/float to represent monetary fields in Java class, but use BigDecimal to take care of the arithmetic (i.e. convert double/float to BigDecimal before any arithmetic) and equal-checking?
The reason is to save some space. And I really see lots of projects are using double/float to represent the monetary fields.
Is there any pitfall for this? Thanks in advance.
If the only use of
double
is to store decimal values, then yes, you can under some conditions: if you can guarantee that your values have no more than 15 decimal digits, then converting a value todouble
(53 bits of precision) and converting thedouble
back to decimal with 15-digit precision (or less) will give you the original value, i.e. without any loss, from an application of David Matula's theorem proved in his article In-and-out conversions. Note that for this result to be applicable, the conversions must be done with correct rounding.Note however that a
double
may not be the best choice: monetary values are generally expressed not in floating point, but in fixed point with a few digits (p) after the decimal point, and in this case, converting the value to an integer with a scaling by 10^p and storing this integer (as others suggested) is better.Pit fall is that floats/doubles can not store all values without losing precision. Even if you do your use
BigDecimal
and preserve precision during calculations, you are still storing the end product as a float/double.The "proper" solution to this, in my experience, is to store monetary values as integers (e.g.
Long
) representing thousands of a dollar. This gives sufficient resolution for most tasks, e.g. interest accruement, while side stepping the problem of using floats/doubles. As an added "bonus", this requires about the same amount of storage as floats/doubles.long
will be much better choice thandouble
/float
.Are you sure that using
BigDecimal
type will be a real bottleneck?No, you can't.
Suppose
double
is enough to store two valuesx
andy
. Then you convert them to safeBigDecimal
and multiple them. The result is accurate, however if you store the multiplication result back indouble
, chances are you will loose the precision. Proof:Results:
x
andy
are accurate, as well as the multiplication usingBigDecimal
. However after casting back todouble
we loose least significant digits.I would also recommend that you use nothing but BigDecimal for ALL arithmetic that may involve currency.
Make sure that you always use the String constructor of BigDecimal. Why? Try the following code in a JUnit test:
You get the following output:
The truth is, you cannot store EXACTLY 0.01 as a 'double' amount. Only BigDecimal stores the number you require EXACTLY as you want it.
And remember that BigDecimal is immutable. The following will compile:
but the resulting output will be:
That's because you need to assign the result to a new (or the same) BigDecimal variable.
In other words:
What is acceptable depends on your project. You can use double and long in some projects may be expected to do so. However in other projects, this is considered unacceptable. As a double you can represent values up to 70,000,000,000,000.00 to the cent (larger than the US national debt), with fixed place long you can represent 90,000,000,000,000,000.00 accurately.
If you have to deal with hyper-inflationary currencies (a bad idea in any case) but for some reason still need to account for every cent, use BigDecimal.
If you use double or long or BigDecimal, you must round the result. How you do this varies with each data type and BigDecimal is the least error prone as you are requires to specify what rounding and the precision for different operations. With double or long, you are left to your own devices.