gpt4 book ai didi

java.lang.ArithmeticException : Non-terminating decimal expansion

转载 作者:行者123 更新时间:2023-12-01 22:11:27 25 4
gpt4 key购买 nike

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);

}

最佳答案

问题是因为除法结果产生一个非终止分数。像 1/3 这样的东西会产生 0.3333.... ,它不会终止。

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

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

关于java.lang.ArithmeticException : Non-terminating decimal expansion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31737681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com