Calculation methods of BigDecimal type

1. Addition (add)

Use the add method to add two BigDecimal objects.

BigDecimal num1 = new BigDecimal("10.5");
BigDecimal num2 = new BigDecimal("20.3");
BigDecimal result = num1.add(num2);
System.out.println("加法结果:" + result); // 输出:加法结果:30.8

2. Subtract

Use the subtract method to subtract one BigDecimal object from another.

BigDecimal num1 = new BigDecimal("30.8");
BigDecimal num2 = new BigDecimal("10.5");
BigDecimal result = num1.subtract(num2);
System.out.println("减法结果:" + result); // 输出:减法结果:20.3

3. Multiply

Use the multiply method to multiply two BigDecimal objects.

BigDecimal num1 = new BigDecimal("2.5");
BigDecimal num2 = new BigDecimal("3.2");
BigDecimal result = num1.multiply(num2);
System.out.println("乘法结果:" + result); // 输出:乘法结果:8.0

4. Division (divide)

Use the divide method to divide one BigDecimal object by another. Rounding rules need to be specified, such as keeping the number of decimal places.

BigDecimal num1 = new BigDecimal("10.0");
BigDecimal num2 = new BigDecimal("3.0");
BigDecimal result = num1.divide(num2, 2, RoundingMode.HALF_UP);
System.out.println("除法结果:" + result); // 输出:除法结果:3.33

5. Take the remainder (remainder)

Use the remainder method to get the remainder of dividing one BigDecimal object by another.

BigDecimal num1 = new BigDecimal("10.0");
BigDecimal num2 = new BigDecimal("3.0");
BigDecimal remainder = num1.remainder(num2);
System.out.println("余数:" + remainder); // 输出:余数:1.0

6. Power operation (pow)

The pow method returns the nth power of the current BigDecimal

BigDecimal base = new BigDecimal("2");
int exponent = 3;
BigDecimal result = base.pow(exponent);
System.out.println("幂运算结果:" + result); // 输出 8

7. Compare (compareTo)

Use the compareTo method to compare the sizes of two BigDecimal objects. The return value is a negative, zero, or positive number, indicating that the first object is less than, equal to, or greater than the second object, respectively.

BigDecimal num1 = new BigDecimal("10.0");
BigDecimal num2 = new BigDecimal("15.0");
int comparisonResult = num1.compareTo(num2);
if (comparisonResult < 0) {
    
    
    System.out.println("num1 小于 num2");
} else if (comparisonResult == 0) {
    
    
    System.out.println("num1 等于 num2");
} else {
    
    
    System.out.println("num1 大于 num2");
}

8. Take the maximum value (max)

Selects the largest value from multiple BigDecimal objects.

BigDecimal num1 = new BigDecimal("10.5");
BigDecimal num2 = new BigDecimal("20.3");
BigDecimal maxNum = num1.max(num2);
System.out.println("Max: " + maxNum); // 输出:Max: 20.3

9. Take the minimum value (min)

Selects the smallest value from multiple BigDecimal objects.

BigDecimal num1 = new BigDecimal("10.5");
BigDecimal num2 = new BigDecimal("20.3");
BigDecimal minNum = num1.min(num2);
System.out.println("Min: " + minNum); // 输出:Min: 10.5

10. Absolute value (abs)

Returns the absolute value of a BigDecimal object.

BigDecimal num = new BigDecimal("-10.5");
BigDecimal absNum = num.abs();
System.out.println("Absolute Value: " + absNum); // 输出:Absolute Value: 10.5

11. Take negative numbers (negate)

Returns the negative number of a BigDecimal object.

BigDecimal num = new BigDecimal("10.5");
BigDecimal negNum = num.negate();
System.out.println("Negative Value: " + negNum); // 输出:Negati

These are some common mathematical calculation methods of the BigDecimal class. Using BigDecimal can avoid precision problems in floating point operations, and is especially suitable for financial and scientific applications that require high-precision calculations. When performing calculations with BigDecimal, extreme care should be taken to handle exceptions such as division by zero or other conditions that may cause exceptions.

Guess you like

Origin blog.csdn.net/weixin_53902288/article/details/132625125