Big Decimal

BigDecimal is a class in java.math package. It is also supported in java.sql.ResultSet class with a getBigDecimal method.

Do you have any experiences with this?


Special Decimal classes avoid the problems of base-2 representation of base-10 fractions. You see these kind of binary problems when your old calculator told you something like 10/2 = 4.9999999999.

See similar Decimal class in DotNet. In Perl there are two classes, MaTH::BigRat? for fractions, and MaTH::BigFloat? for floating point, and MaTH::BigInt for arbitrary precision integers.


BigDecimal and BigInteger are used for precise arithmetical work, including cryptographical work. However, BigDecimal is used more for financial work. The rounding methods support banker's rounding (round toward even). The primitive float and double classes aren't really suitable.


We use BigDecimal for financial work, however, like many other JavaLanguage core classes, i.e., GregorianCalendar?, it is clumsy and needs to be contained within a simpler interface.


IBM Fellow MikeCowlishaw designed a BigDecimal class which was, I am told, a significant improvement over the Java class that preceded it (I never used the old one). It is now available as of Java 5.0. A 2004 presentation describing it is at http://www.softwaresummit.com/2004/speakers/CowlishawBigDecimal.pdf. The specification is at http://www.jcp.org/en/jsr/detail?id=13.

We used an early version of it for a Brokerage application, and the function was excellent, but obviously you have to sacrifice some performance if you stay in pure Java - an add method (say) is a fairly complex piece of Java code. In the presentation, Mike talks about IBM's plans to support this function in hardware based on the agreed IEEE 754r standard. --PaulMorrison


See also: FloatingPointCurrency


CategoryJava


EditText of this page (last edited December 14, 2005) or FindPage with title or text search