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);
1617
//保留小數字與解釋
1819
/**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));
25 system.out.println(b2.setscale(2, bigdecimal.round_up));
26 system.out.println(b3.setscale(2, bigdecimal.round_up));
27 system.out.println(b4.setscale(2, bigdecimal.round_up));
28 system.out.println(b5.setscale(2, bigdecimal.round_up));
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));
35 system.out.println(b2.setscale(2, bigdecimal.round_down));
36 system.out.println(b3.setscale(2, bigdecimal.round_down));
37 system.out.println(b4.setscale(2, bigdecimal.round_down));
38 system.out.println(b5.setscale(2, bigdecimal.round_down));
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));
46 system.out.println(b2.setscale(2, bigdecimal.round_ceiling));
47 system.out.println(b3.setscale(2, bigdecimal.round_ceiling));
48 system.out.println(b4.setscale(2, bigdecimal.round_ceiling));
49 system.out.println(b5.setscale(2, bigdecimal.round_ceiling));
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));
57 system.out.println(b2.setscale(2, bigdecimal.round_floor));
58 system.out.println(b3.setscale(2, bigdecimal.round_floor));
59 system.out.println(b4.setscale(2, bigdecimal.round_floor));
60 system.out.println(b5.setscale(2, bigdecimal.round_floor));
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));
67 system.out.println(b2.setscale(2, bigdecimal.round_half_up));
68 system.out.println(b3.setscale(2, bigdecimal.round_half_up));
69 system.out.println(b4.setscale(2, bigdecimal.round_half_up));
70 system.out.println(b5.setscale(2, bigdecimal.round_half_up));
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));
77 system.out.println(b2.setscale(2, bigdecimal.round_half_down));
78 system.out.println(b3.setscale(2, bigdecimal.round_half_down));
79 system.out.println(b4.setscale(2, bigdecimal.round_half_down));
80 system.out.println(b5.setscale(2, bigdecimal.round_half_down));
BigDecimal 加 減 乘 除 比較
bigdecimal 多用於商業計算,或者較大數值得計算 bigdecimal bignum1 newbigdecimal 10 bigdecimal bignum2 new bigdecimal 5 bigdecimal bignum3 null 加法 bignum3 bignum1.add bi...
BigDecimal加減乘除計算
bigdecimal num1 new bigdecimal 0.005 bigdecimal num2 new bigdecimal 1000000 bigdecimal num3 new bigdecimal 1000000 盡量用字串的形式初始化 bigdecimal num12 new bi...
BigDecimal的運算 加減乘除
一 建立乙個bigdecimal物件。這裡對比了兩種形式,第一種直接value寫數字的值,第二種用string來表示 bigdecimal num1 new bigdecimal 0.005 bigdecimal num2 new bigdecimal 1000000 bigdecimal num3...