bigdecimal加减乘除运算案例
时间: 2023-04-09 10:03:10 浏览: 148
我可以回答这个问题。下面是一个 BigDecimal 加减乘除运算的案例:
import java.math.BigDecimal;
public class BigDecimalExample {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("10.5");
BigDecimal num2 = new BigDecimal("5");
// 加法
BigDecimal sum = num1.add(num2);
System.out.println("Sum of " + num1 + " and " + num2 + " is " + sum);
// 减法
BigDecimal difference = num1.subtract(num2);
System.out.println("Difference between " + num1 + " and " + num2 + " is " + difference);
// 乘法
BigDecimal product = num1.multiply(num2);
System.out.println("Product of " + num1 + " and " + num2 + " is " + product);
// 除法
BigDecimal quotient = num1.divide(num2);
System.out.println("Quotient of " + num1 + " and " + num2 + " is " + quotient);
}
}
输出结果为:
Sum of 10.5 and 5 is 15.5
Difference between 10.5 and 5 is 5.5
Product of 10.5 and 5 is 52.5
Quotient of 10.5 and 5 is 2.1
阅读全文