1.BigDecimal比较大小
public static void main(String[] args) {
BigDecimal a = new BigDecimal(10);
BigDecimal b = new BigDecimal(5);
//比较大小
if (a.compareTo(b) == 0)
System.out.println("a = b");
if (a.compareTo(b) == -1)
System.out.println("a < b");
if (a.compareTo(b) == 1)
System.out.println("a > b");
if (a.compareTo(b) != 0)
System.out.println("a != b");
if (a.compareTo(b) != -1)
System.out.println("a >= b");
if (a.compareTo(b) != 1)
System.out.println("a <= b");
}
2.BigDecimal加减乘除运算
//加法:.add
//减法:.subtract
//乘法:.multiply
//除法:.divide
//绝对值:.abs()
//小数位数:scale
BigDecimal num1 = new BigDecimal (9);
BigDecimal num2 = new BigDecimal (11);
//加法 BigDecimal result1 = num1.add(num2);
//减法 BigDecimal result2 = num1.subtract(num2);
//乘法 BigDecimal result3 = num1.multiply(num2);
//绝对值 BigDecimal result4 = num3.abs();
//除法 (BigDecimal divisor 除数, int scale 精确小数位, int roundingMode 舍入模式)
BigDecimal result5 = num2.divide(num1,20,BigDecimal.ROUND_HALF_UP);