if (builderAggregateCalculationsType == AggregateCalculationsType.AGGREGATE_AVERAGE) {
            BigDecimal quarterDivisor = new BigDecimal(3);
            quarterDivisor = quarterDivisor.setScale(
                    aggregateCalculationsNumberOfDecimalPlaces,
                    RoundingMode.HALF_UP);
            firstQuarterSum = firstQuarterSum.setScale(
                    aggregateCalculationsNumberOfDecimalPlaces,
                    RoundingMode.HALF_UP);
            secondQuarterSum = secondQuarterSum.setScale(
                    aggregateCalculationsNumberOfDecimalPlaces,
                    RoundingMode.HALF_UP);

            firstQuarterSum = firstQuarterSum.divide(quarterDivisor);
            secondQuarterSum = secondQuarterSum.divide(quarterDivisor);

        }

最佳答案

问题在于,除法结果产生了Non-terminating分数。像1/3这样的东西会产生0.3333....,它不会终止。

如果可以允许某些错误,则可以指定RoundingMode并使用它,

firstQuarterSum.divide(quarterDivisor, RoundingMode.HALF_UP); // Or any other rounding mode you prefer.

关于java - java.lang.ArithmeticException:无终止的十进制扩展,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31737681/

10-10 14:41