BigDecimal operator support in Java 7 (original) (raw)

One of the candidate items for Java 7 helpfully listed by Alex Miller is BigDecimal operator support. I'm keen to see this go in as it would really help customers who use decimals.

Mike Cowlishaw's General Decimal Arithmetic page is a rich source of information on this subject. It contains an example 'telco' benchmark coded in Java and C# which provide a helpful comparison when thinking about syntax support for BigDecimal.

Take the following sequence (massaged a little for display here) from the Java version:

b = p.multiply(basetax); b = b.setScale(2, BigDecimal.ROUND_DOWN); sumB = sumB.add(b); t = p.add(b);

if (calltype != 0) { d = p.multiply(disttax); d = d.setScale(2, BigDecimal.ROUND_DOWN); sumD = sumD.add(d); t = t.add(d); }

and compare the C# version for readability:

b = p * basetax * 100; b = Decimal.Truncate(b) / 100; sumB = sumB + b; t = p + b;

if (calltype != 0) { d = p * disttax * 100; d = Decimal.Truncate(d) / 100; sumD = sumD + d; t = t + d; }

Enough said?