Tuesday, August 10, 2010

Default Round Mode when Setting a BigDecimal Scale

If you are not familiar with BigDecimal and want to set its scale to, say 2, you might just call myBigDecimal.setScale(2) assuming the BigDecimal will round excess decimals automatically for us. Unforrtunately BigDecima's default rounding mode is not rounding anything. So if your big decimal is 1.234, you will get a "Rounding necessary" exception.

Almost all developers I worked with assumed the setScale() will do some automatically rounding. They didn't realize it until they got a the above run time exception.
Since it is so common(based on what I experienced), why shouldn't it be default to a rounding mode such as ROUND_HAVE_UP?

No comments:

Post a Comment