代码一:
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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复