多个bigdecimal相减
时间: 2023-10-20 19:01:23 浏览: 72
BigDecimal详解、代码示例和经常遇到的坑
如果你想要实现多个BigDecimal相减,可以使用以下方法:
```java
public static BigDecimal getBigDecimalDifference(BigDecimal i, BigDecimal... arg) {
BigDecimal difference = i;
for (BigDecimal b : arg) {
difference = difference.subtract(b);
}
return difference;
}
```
你可以将需要相减的BigDecimal作为参数传入这个方法,然后通过循环遍历每个BigDecimal,使用subtract方法进行相减操作。最后返回得到的差值。
例如,假设有以下的代码:
```java
BigDecimal b1 = new BigDecimal("10");
BigDecimal b2 = new BigDecimal("5");
BigDecimal b3 = new BigDecimal("3");
BigDecimal result = getBigDecimalDifference(b1, b2, b3);
System.out.println(result); // 输出为2
```
在这个例子中,我们将b1、b2和b3作为参数传入getBigDecimalDifference方法,然后得到的结果为2,即10 - 5 - 3的差值。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [多个BigDecimal数相加相减](https://blog.csdn.net/chesou8240/article/details/100633791)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [BigDecimal用法](https://blog.csdn.net/baidu_41634343/article/details/90439360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文