Intellij提示在舍入BigDecimal Diseman流中



我在Intellij提示的同时在流操作结束时有一个奇怪的问题。

products.stream()
        .filter(
            order ->
                order.getEstimatedRealizationDate().compareTo(begin) > 0
                    && order.getEstimatedRealizationDate().compareTo(end) < 0)
        .map(order -> order.getProduct().getPrice())
        .reduce(ZERO, BigDecimal::add)
        .divide(valueOf(productList.size()))
        .setScale(3, RoundingMode.CEILING);

不管您如何设置舍入,Intellij都会不断声称,分隔操作可能会以artithmeticeCeption的形式以消息的形式出现风险和背光分隔操作的形式。我正在使用任何一个圆()选项。

报告无圆模式的divide()或setScale()的电话 争论。当确切的情况下,此类呼叫可能会导致算术 值不能在结果中表示(例如,因为它具有 非终止小数膨胀)。指定圆形模式 防止算术感受。

我的印象是我尝试了所有可能的变体,但是Intellij并没有放弃。请建议我做错了什么。事先感谢您的每种帮助。

setScale只是返回带有指定比例的 BigDecimal对象。您要使用过载的divide方法

divide(BigDecimal divisor, int scale, RoundingMode roundingMode)

products.stream()
        .filter(
            order ->
                order.getEstimatedRealizationDate().compareTo(begin) > 0
                    && order.getEstimatedRealizationDate().compareTo(end) < 0)
        .map(order -> order.getProduct().getPrice())
        .reduce(ZERO, BigDecimal::add)
        .divide(valueOf(productList.size()), 3, RoundingMode.CEILING);

在此示例中,我使用的是RoundingMode.CEILING,但以下是RoundingMode的文档,以防您要使用其他圆形模式。

最新更新