add(BigDecimal)
时间: 2023-10-21 20:20:51 浏览: 49
BigDecimal is a class in Java that provides support for arbitrary-precision decimal arithmetic. The `add(BigDecimal)` method is used to perform addition with another BigDecimal object. It takes another BigDecimal as a parameter and returns a new BigDecimal representing the sum of the two numbers. Here's an example:
```java
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("10.5");
BigDecimal num2 = new BigDecimal("5.25");
BigDecimal sum = num1.add(num2);
System.out.println("Sum: " + sum);
}
}
```
In this example, we create two BigDecimal objects `num1` and `num2`, representing the numbers 10.5 and 5.25 respectively. We then use the `add()` method to add them together and store the result in the `sum` variable. Finally, we print the sum to the console. The output will be:
```
Sum: 15.75
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)