复制代码
1
2
3
4
5
6
7
8
9@Test public void testBigDecimal(){ BigDecimal decimal1=new BigDecimal("0"); BigDecimal decimal2=new BigDecimal(2); decimal1.add(decimal2); System.out.println(decimal1.add(decimal1)); System.out.println(decimal1.add(decimal2)); }
结果输出:
神不神奇,意不意外,a.add(b) ,a的值没变
而是a.add 方法返回 a+b的值
具体看源码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/** * Returns a {@code BigDecimal} whose value is {@code (this + * augend)}, and whose scale is {@code max(this.scale(), * augend.scale())}. * * @param augend value to be added to this {@code BigDecimal}. * @return {@code this + augend} (返回和) */ 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); } } }
可以看到是return 相加之后的结果,而不是将结果放在当前对象的属性中.
最后
以上就是无聊镜子最近收集整理的关于BigDecimal 的add方法,结果竟然没变??的全部内容,更多相关BigDecimal内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复