关于BigDecimal的一些常用基本操作记录

 1        BigDecimal b1 = new BigDecimal("1.124");
2        BigDecimal b2 = new BigDecimal("1.125");
3        BigDecimal b3 = new BigDecimal("1.126");
4        BigDecimal b4 = new BigDecimal("-1.126");
5        BigDecimal b5 = new BigDecimal("-1.123");
6        //加法
7        b1.add(b2);
8        //减法
9        b1.subtract(b2);
10        //乘法
11        b1.multiply(b2);
12        //除法(关于其小数保留下面统一解释)
13        b1.divide(b3, 2, BigDecimal.ROUND_UP);
14        //比较大小 (通过返回值区分 =0 相等, =1 前者大, =-1 后者大 )
15        b1.compareTo(b2);
16
17        //保留小数位与解释
18
19        /**
20         * 始终对非零舍弃部分前面的数字加1
21         *
22         * b1 1.124, b2 1.125, b3 1.126, b4 -1.126, b5 -1.123
23         */
24        System.out.println(b1.setScale(2, BigDecimal.ROUND_UP));  //1.13
25        System.out.println(b2.setScale(2, BigDecimal.ROUND_UP));  //1.13
26        System.out.println(b3.setScale(2, BigDecimal.ROUND_UP));  //1.13
27        System.out.println(b4.setScale(2, BigDecimal.ROUND_UP));  //-1.13
28        System.out.println(b5.setScale(2, BigDecimal.ROUND_UP));  //-1.13
29        /**
30         * 从不对舍弃部分前面的数字加1
31         *
32         * b1 1.124, b2 1.125, b3 1.126, b4 -1.126, b5 -1.123
33         */
34        System.out.println(b1.setScale(2, BigDecimal.ROUND_DOWN));  //1.12
35        System.out.println(b2.setScale(2, BigDecimal.ROUND_DOWN));  //1.12
36        System.out.println(b3.setScale(2, BigDecimal.ROUND_DOWN));  //1.12
37        System.out.println(b4.setScale(2, BigDecimal.ROUND_DOWN));  //-1.12
38        System.out.println(b5.setScale(2, BigDecimal.ROUND_DOWN));  //-1.12
39        /**
40         * 如果 BigDecimal 为正,则舍入行为与 ROUND_UP 相同
41         * 如果为负,则舍入行为与 ROUND_DOWN 相同
42         *
43         * b1 1.124, b2 1.125, b3 1.126, b4 -1.126, b5 -1.123
44         */
45        System.out.println(b1.setScale(2, BigDecimal.ROUND_CEILING)); //1.13
46        System.out.println(b2.setScale(2, BigDecimal.ROUND_CEILING)); //1.13
47        System.out.println(b3.setScale(2, BigDecimal.ROUND_CEILING)); //1.13
48        System.out.println(b4.setScale(2, BigDecimal.ROUND_CEILING)); //-1.12
49        System.out.println(b5.setScale(2, BigDecimal.ROUND_CEILING)); //-1.12
50        /**
51         * 如果 BigDecimal 为正,则舍入行为与 ROUND_DOWN 相同
52         * 如果为负,则舍入行为与 ROUND_UP 相同
53         *
54         * b1 1.124, b2 1.125, b3 1.126, b4 -1.126, b5 -1.123
55         */
56        System.out.println(b1.setScale(2, BigDecimal.ROUND_FLOOR));  //1.12
57        System.out.println(b2.setScale(2, BigDecimal.ROUND_FLOOR));  //1.12
58        System.out.println(b3.setScale(2, BigDecimal.ROUND_FLOOR));  //1.12
59        System.out.println(b4.setScale(2, BigDecimal.ROUND_FLOOR));  //-1.13
60        System.out.println(b5.setScale(2, BigDecimal.ROUND_FLOOR));  //-1.13
61        /**
62         * 如果舍弃部分 >= 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同
63         *
64         * b1 1.124, b2 1.125, b3 1.126, b4 -1.126, b5 -1.123
65         */
66        System.out.println(b1.setScale(2, BigDecimal.ROUND_HALF_UP));  //1.12
67        System.out.println(b2.setScale(2, BigDecimal.ROUND_HALF_UP));  //1.13
68        System.out.println(b3.setScale(2, BigDecimal.ROUND_HALF_UP));  //1.13
69        System.out.println(b4.setScale(2, BigDecimal.ROUND_HALF_UP));  //-1.13
70        System.out.println(b5.setScale(2, BigDecimal.ROUND_HALF_UP));  //-1.12
71        /**
72         * 如果舍弃部分 > 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同(五舍六入)。
73         *
74         * b1 1.124, b2 1.125, b3 1.126, b4 -1.126, b5 -1.123
75         */
76        System.out.println(b1.setScale(2, BigDecimal.ROUND_HALF_DOWN));  //1.12
77        System.out.println(b2.setScale(2, BigDecimal.ROUND_HALF_DOWN));  //1.12
78        System.out.println(b3.setScale(2, BigDecimal.ROUND_HALF_DOWN));  //1.13
79        System.out.println(b4.setScale(2, BigDecimal.ROUND_HALF_DOWN));  //-1.13
80        System.out.println(b5.setScale(2, BigDecimal.ROUND_HALF_DOWN));  //-1.12
05-16 12:47