日常對于金額計算,應該都是用的bigdecimal
,可是苦于沒有好的工具類方法,現在貢獻一個我正在用的對于數字計算的工具類,項目中就是用的這個,簡單粗暴好用,話不多說,代碼奉上(該工具類需要引入google的一個jar,com.google.common.base.optional
,具體maven
引入看文章末尾):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
import java.math.bigdecimal; public class numberarithmeticutils { /** * bigdecimal的加法運算封裝 * @param b1 * @param bn * @return */ public static bigdecimal safeadd(bigdecimal b1, bigdecimal... bn) { if ( null == b1) { b1 = bigdecimal.zero; } if ( null != bn) { for (bigdecimal b : bn) { b1 = b1.add( null == b ? bigdecimal.zero : b); } } return b1; } /** * integer加法運算的封裝 * @param b1 第一個數 * @param bn 需要加的加法數組 * @注 : optional 是屬于com.google.common.base.optional<t> 下面的class * @return */ public static integer safeadd(integer b1, integer... bn) { if ( null == b1) { b1 = 0 ; } integer r = b1; if ( null != bn) { for (integer b : bn) { r += optional.fromnullable(b).or( 0 ); } } return r > 0 ? r : 0 ; } /** * 計算金額方法 * @param b1 * @param bn * @return */ public static bigdecimal safesubtract(bigdecimal b1, bigdecimal... bn) { return safesubtract( true , b1, bn); } /** * bigdecimal的安全減法運算 * @param iszero 減法結果為負數時是否返回0,true是返回0(金額計算時使用),false是返回負數結果 * @param b1 被減數 * @param bn 需要減的減數數組 * @return */ public static bigdecimal safesubtract( boolean iszero, bigdecimal b1, bigdecimal... bn) { if ( null == b1) { b1 = bigdecimal.zero; } bigdecimal r = b1; if ( null != bn) { for (bigdecimal b : bn) { r = r.subtract(( null == b ? bigdecimal.zero : b)); } } return iszero ? (r.compareto(bigdecimal.zero) == - 1 ? bigdecimal.zero : r) : r; } /** * 整型的減法運算,小于0時返回0 * @param b1 * @param bn * @return */ public static integer safesubtract(integer b1, integer... bn) { if ( null == b1) { b1 = 0 ; } integer r = b1; if ( null != bn) { for (integer b : bn) { r -= optional.fromnullable(b).or( 0 ); } } return null != r && r > 0 ? r : 0 ; } /** * 金額除法計算,返回2位小數(具體的返回多少位大家自己看著改吧) * @param b1 * @param b2 * @return */ public static <t extends number> bigdecimal safedivide(t b1, t b2){ return safedivide(b1, b2, bigdecimal.zero); } /** * bigdecimal的除法運算封裝,如果除數或者被除數為0,返回默認值 * 默認返回小數位后2位,用于金額計算 * @param b1 * @param b2 * @param defaultvalue * @return */ public static <t extends number> bigdecimal safedivide(t b1, t b2, bigdecimal defaultvalue) { if ( null == b1 || null == b2) { return defaultvalue; } try { return bigdecimal.valueof(b1.doublevalue()).divide(bigdecimal.valueof(b2.doublevalue()), 2 , bigdecimal.round_half_up); } catch (exception e) { return defaultvalue; } } /** * bigdecimal的乘法運算封裝 * @param b1 * @param b2 * @return */ public static <t extends number> bigdecimal safemultiply(t b1, t b2) { if ( null == b1 || null == b2) { return bigdecimal.zero; } return bigdecimal.valueof(b1.doublevalue()).multiply(bigdecimal.valueof(b2.doublevalue())).setscale( 2 , bigdecimal.round_half_up); } } |
optional所在的jar以及版本:guava-18.0.ar
pom.xml配置:
1
2
3
4
5
6
|
<!-- https: //mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupid>com.google.guava</groupid> <artifactid>guava</artifactid> <version> 18.0 </version> </dependency> |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/moneyshi/article/details/65445820