无法在 Groovy 中添加 BigDecimal 值
Unable to add BigDecimal value in Groovy
def total = new BigDecimal("0.00");
total.add(new BigDecimal("1"));
println total;
考虑上面的以下代码:此代码的输出为零。
为什么?
您必须分配结果(请参阅下面的文档)。或得到 groovy:
def total = 0.0G + 1G
assert total.getClass() == BigDecimal
assert total==1.0G
total += 1.0G
assert total.getClass() == BigDecimal
assert total==2.0G
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#add%28java.math.BigDecimal%29
public BigDecimal add(BigDecimal augend)
Returns a BigDecimal whose value is (this
+ augend
), and whose scale is max(this
.scale(), augend
.scale()).
Parameters:
augend
- value to be added to this
BigDecimal.
Returns:
this
+ augend
def total = new BigDecimal("0.00");
total.add(new BigDecimal("1"));
println total;
考虑上面的以下代码:此代码的输出为零。
为什么?
您必须分配结果(请参阅下面的文档)。或得到 groovy:
def total = 0.0G + 1G
assert total.getClass() == BigDecimal
assert total==1.0G
total += 1.0G
assert total.getClass() == BigDecimal
assert total==2.0G
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#add%28java.math.BigDecimal%29
public BigDecimal add(BigDecimal augend)
Returns a BigDecimal whose value is (
this
+augend
), and whose scale is max(this
.scale(),augend
.scale()).Parameters:
augend
- value to be added tothis
BigDecimal.Returns:
this
+augend