加减乘除
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c = a.add(b);
BigDecimal d = a.subtract(b);
BigDecimal e = a.multiply(b);
BigDecimal f = a.divide(b,2,BigDecimal.ROUND_HALF_UP);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
a.compareTo(b)
BigDecimal[] mod = a.divideAndRemainder(b);
BigDecimal mod0 = mod[0];
BigDecimal mod1 = mod[1];
System.out.println(mod0);
System.out.println(mod1);
小数:
BigDecimal h = new BigDecimal("1.23456789");
h.setScale(1,BigDecimal.ROUND_DOWN);//直接删除多余的小数位,如1.2
h.setScale(1,BigDecimal.ROUND_UP);//进位处理,1.3
h.setScale(3,BigDecimal.ROUND_HALF_UP);//四舍五入(5则向上入),1.235
h.setScale(3,BigDecimal.ROUND_HALF_DOWN);//五舍六入(5则向下舍,6则向上入),1.234
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.