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.
*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.
"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
subject: Rounding BigDecimal Calculation Yielded Wrong Result