1、加减乘除


1.1、加法: add()

1.2、减法:subtract()

1.3、乘法:multiply()

1.4、除法:divide()

1.5、绝对值:abs()

示例:

totalMemory = new BigDecimal(memory).subtract(decimal.multiply(new BigDecimal(memory))).divide(new BigDecimal("1024")).setScale(0,BigDecimal.ROUND_DOWN);

2、四舍五入


2.1、ROUND_DOWN

    BigDecimal bigDecimal1 = new BigDecimal("3.222452").setScale(2, BigDecimal.ROUND_DOWN);
    BigDecimal bigDecimal2 = new BigDecimal("-3.222452").setScale(2, BigDecimal.ROUND_DOWN);
    BigDecimal bigDecimal3 = new BigDecimal("3.225452").setScale(2, BigDecimal.ROUND_DOWN);
    System.out.println("----------------ROUND_DOWN-------------");
    System.out.println(bigDecimal1);//3.22 直接去掉多余的位数
    System.out.println(bigDecimal2);//-3.22 直接去掉多余的位数
    System.out.println(bigDecimal3);//3.22 无论正负,直接去掉多余的位数


2.2、ROUND_UP

    BigDecimal bigDecimal4 = new BigDecimal("3.236452").setScale(2, BigDecimal.ROUND_UP);
    BigDecimal bigDecimal5 = new BigDecimal("-3.226452").setScale(2, BigDecimal.ROUND_UP);
    BigDecimal bigDecimal6 = new BigDecimal("3.235452").setScale(2, BigDecimal.ROUND_UP);
    BigDecimal bigDecimal7 = new BigDecimal("-3.225452").setScale(2, BigDecimal.ROUND_UP);
    System.out.println("----------------ROUND_UP-------------");
    System.out.println(bigDecimal4);// 3.24 无论正负,当后一位大于等于.5时,进位
    System.out.println(bigDecimal5);//-3.23
    System.out.println(bigDecimal6);//3.24
    System.out.println(bigDecimal7);//-3.23


2.3、ROUND_CEILING

   BigDecimal bigDecimal8 = new BigDecimal("3.236452").setScale(2, BigDecimal.ROUND_CEILING);
   BigDecimal bigDecimal9 = new BigDecimal("-3.226452").setScale(2, BigDecimal.ROUND_CEILING);
   BigDecimal bigDecimal10 = new BigDecimal("3.235452").setScale(2, BigDecimal.ROUND_CEILING);
   BigDecimal bigDecimal11 = new BigDecimal("-3.225452").setScale(2, BigDecimal.ROUND_CEILING);
   System.out.println("-----------------ROUND_CEILING--------------");
   System.out.println(bigDecimal8);//3.24 正数,等同于ROUND_UP
   System.out.println(bigDecimal9);//-3.22 负数,等同于ROUND_DOWN
   System.out.println(bigDecimal10);//3.24
   System.out.println(bigDecimal11);//-3.22


2.4、ROUND_FLOOR

   BigDecimal bigDecimal12 = new BigDecimal("3.236452").setScale(2, BigDecimal.ROUND_FLOOR);
   BigDecimal bigDecimal13 = new BigDecimal("-3.226452").setScale(2, BigDecimal.ROUND_FLOOR);
   BigDecimal bigDecimal14 = new BigDecimal("3.235452").setScale(2, BigDecimal.ROUND_FLOOR);
   BigDecimal bigDecimal15 = new BigDecimal("-3.225452").setScale(2, BigDecimal.ROUND_FLOOR);
   System.out.println("-------------ROUND_FLOOR-----------");
   System.out.println(bigDecimal12);// 3.23 正数,等同于ROUND_DOWN
   System.out.println(bigDecimal13);// -3.23负数,等同于ROUND_UP
   System.out.println(bigDecimal14);// 3.23
   System.out.println(bigDecimal15);// -3.23


2.5、ROUND_HALF_UP

   BigDecimal bigDecimal16 = new BigDecimal("3.236452").setScale(2, BigDecimal.ROUND_HALF_UP);
   BigDecimal bigDecimal17 = new BigDecimal("-3.226452").setScale(2, BigDecimal.ROUND_HALF_UP);
   BigDecimal bigDecimal18 = new BigDecimal("3.235452").setScale(2, BigDecimal.ROUND_HALF_UP);
   BigDecimal bigDecimal19 = new BigDecimal("-3.225").setScale(2, BigDecimal.ROUND_HALF_UP);
   System.out.println("------------ROUND_HALF_UP------------");
   System.out.println(bigDecimal16);// 3.24 无论正负,舍弃部分大于等于.5,进位
   System.out.println(bigDecimal17);// -3.23
   System.out.println(bigDecimal18);// 3.24
   System.out.println(bigDecimal19);// -3.23


2.6、ROUND_HALF_DOWN

   BigDecimal bigDecimal20 = new BigDecimal("3.236452").setScale(2, BigDecimal.ROUND_HALF_DOWN);
   BigDecimal bigDecimal21 = new BigDecimal("-3.225452").setScale(2, BigDecimal.ROUND_HALF_DOWN);
   BigDecimal bigDecimal22 = new BigDecimal("3.234452").setScale(2, BigDecimal.ROUND_HALF_DOWN);
   BigDecimal bigDecimal23 = new BigDecimal("-3.225").setScale(2, BigDecimal.ROUND_HALF_DOWN);
   System.out.println("------------ROUND_HALF_DOWN------------");
   System.out.println(bigDecimal20);// 3.24 去掉的部分需要大于.5,进位
   System.out.println(bigDecimal21);// -3.23  去掉的部分为.5452 大于.5 进位
   System.out.println(bigDecimal22);//3.23
   System.out.println(bigDecimal23);// -3.22  由于等于.5 所以直接去掉