I want to use BigDecimal in some calculations. Imagine that in some intermediate step there is the following division new BigDecimal(1).divide(new BigDecimal(4))
. I understand that in this case the scale is calculated automatically and the result is 0.25
. But Imagine that in some cases this fraction cannot be represented as a decimal expanstion like in new BigDecimal(1).divide(new BigDecimal(3))
then this exception is thrown java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
cause no scale was set.
My question is: I don't want to force a fixed scale when it is not necessary, but I don't want it to throw an exception either. How can I accomplish that? Is there a way to say "Use this scale when you don't know what to do"?. If so, how can I do that, and what would be a reasonable value for the scale in an interest calculation?