Originally posted @ gratiartis.org on April 6 2007
As a developer of financial systems, I’m constantly disappointed by how difficult it is to perform precise decimal arithmetic in Java. It’s fairly common knowledge that the use of double is not much use for financial calculations. As a quick example, take a look at the following piece of code.
public static void main(String arg[]) {
BigDecimal bd = new BigDecimal("58.99").add(new BigDecimal("0.99"));
System.out.println("BigDecimal result: " + bd);
double d = 58.99 + 0.99;
System.out.println("Double result: " + d);
}
You would have thought both results should be the same. However, you end up with:
BigDecimal result: 59.98
Double result: 59.980000000000004
This is caused by the fact that in Java, operations on double primitive uses floating point arithmetic. The use of floating point arithmetic does make it relatively fast to perform the operations. The approximate nature of them may not matter in all situations, but for financial calculations it can cause all sorts of problems.
Continue reading “Groovy as a financial domain language?” →