我是靠谱客的博主 饱满鱼,这篇文章主要介绍BigDecimal.add方法不生效,现在分享给大家,希望可以做个参考。

代码一:

BigDecimal theory_score = new BigDecimal(0.0);
theory_score.add(bdJobContentAnswer.getScore());
//bdJobContentAnswer.getScore 为20.0

结果:theory_score = 0.0
代码二:

BigDecimal theory_score = new BigDecimal(0.0);
theory_score = theory_score.add(bdJobContentAnswer.getScore());

结果:theory_score = 20.0

原因:通过看BigDecimal.add()方法的源码发现

public BigDecimal add(BigDecimal augend) {
        if (this.intCompact != INFLATED) {
            if ((augend.intCompact != INFLATED)) {
                return add(this.intCompact, this.scale, augend.intCompact, augend.scale);
            } else {
                return add(this.intCompact, this.scale, augend.intVal, augend.scale);
            }
        } else {
            if ((augend.intCompact != INFLATED)) {
                return add(augend.intCompact, augend.scale, this.intVal, this.scale);
            } else {
                return add(this.intVal, this.scale, augend.intVal, augend.scale);
            }
        }
    }

add方法返回的是一个BigDecimal对象,所以是不能改变自身值得,只能用一个新对象去接收add方法改变后对象

最后

以上就是饱满鱼最近收集整理的关于BigDecimal.add方法不生效的全部内容,更多相关BigDecimal内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(144)

评论列表共有 0 条评论

立即
投稿
返回
顶部