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);
}
答案 0 :(得分:3)
问题是因为除法结果产生Non-terminating
分数。像1/3
这样的内容会生成0.3333....
,但不会终止。
如果您允许某些错误,可以指定RoundingMode并使用它,
firstQuarterSum.divide(quarterDivisor, RoundingMode.HALF_UP); // Or any other rounding mode you prefer.