programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Rounding BigDecimal Calculation Yielded Wrong Result

Ranch Hand
Posts: 110
• Number of slices to send:
Optional 'thank-you' note:
Hello All,

The first println yielded 4.15
The second println yielded 4.980

Doing it manually, the result should be indeed 4.98

Does this imply that we should not use BigDecimal for calculations?

[ September 22, 2004: Message edited by: James Gordon ]

Ranch Hand
Posts: 1392
• Number of slices to send:
Optional 'thank-you' note:
Hi James,

I think it's still possible to use BigDecimal for the calculation if the scale value is set higher. Then round off the final value to 2 or 3 decimal places.

Joyce
[ September 22, 2004: Message edited by: Joyce Lee ]

Ranch Hand
Posts: 374
• Number of slices to send:
Optional 'thank-you' note:
Keep in mind that you have yourself specified the number of significant digits to carry. BigDecimal has another divide method that does not require a scale:

public BigDecimal divide(BigDecimal val,
int roundingMode)Returns a BigDecimal whose value is (this / val), and whose scale is this.scale(). If rounding must be performed to generate a result with the given scale, the specified rounding mode is applied.

I have yet to see this method fail in standard calculations with a known-to-be-precise answer.

HOWEVER...

*You can always cover yourself with a NumberFormat that only allows 2 decimal places.
*BigDecimal also provides many rounding modes to choose from,
*You can always modify operator order to clarify what the result should be
*Your operands define the inital scale of the result

Play around with the following code some. Note that I don't specify a scale.

If you pass "415" without the ".0" you'll get 5 as a result -- one of the operands had a zero scale.

Anyway, good luck and have fun.