forked from leavesCZY/AndroidGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigDecimalUtil.java
More file actions
224 lines (184 loc) · 7.72 KB
/
BigDecimalUtil.java
File metadata and controls
224 lines (184 loc) · 7.72 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalUtil {
public static void main(String[] args) {
System.out.println("比较大小");
System.out.println(compare(2.2222, 3.333)); //-1
System.out.println(compare(4.2222, 3.333)); //1
System.out.println(compare(2.2222, 2.2222)); //0
System.out.println("加");
System.out.println(add(11.22, 22, 334, 44.44, 32.112121)); //443.772121
System.out.println(add("11.22", "22", "334", "44.44", "32.112121")); //443.772121
System.out.println("减");
System.out.println(subtract(10, 0.001, 0.0001)); //9.9989
System.out.println(subtract(11.22, 22, 334, 44.44, 32.11212)); //-421.33212
System.out.println(subtract("11.22", "22", "334", "44.44", "32.11212")); //-421.33212
System.out.println("乘");
System.out.println(multiply(11.221, 22.22222222)); //249.35555553062
System.out.println("除");
System.out.println(divideDown(12.1299, 2, 6)); //6.06495
System.out.println(divideDown(12.1299, 2, 5)); //6.06495
System.out.println(divideDown(12.1299, 2, 4)); //6.064
System.out.println(divideHalfUp(12.1299, 2, 3)); //6.065
System.out.println(divideHalfUp(12.1299, 2, 2)); //6.06
System.out.println(divideHalfUp(12.1299, 2, 1)); //6.1
System.out.println("绝对值");
System.out.println(abs(-22.22121141141241)); //22.22121141141241
System.out.println("最大值");
System.out.println(max(22.22, 22.2222));
System.out.println("最小值");
System.out.println(min(22.22, 22.233));
}
public static int compare(int value1, int value2) {
return compare(String.valueOf(value1), String.valueOf(value2));
}
public static int compare(double value1, double value2) {
return compare(String.valueOf(value1), String.valueOf(value2));
}
public static int compare(String value1, String value2) {
BigDecimal bigDecimal = new BigDecimal(value1);
BigDecimal bigDecimal1 = new BigDecimal(value2);
return bigDecimal.compareTo(bigDecimal1);
}
public static double add(int... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = BigDecimal.ZERO;
for (double value : values) {
total = total.add(new BigDecimal(String.valueOf(value)));
}
return total.doubleValue();
}
public static double add(double... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = BigDecimal.ZERO;
for (double value : values) {
total = total.add(new BigDecimal(String.valueOf(value)));
}
return total.doubleValue();
}
public static double add(String... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = BigDecimal.ZERO;
for (String value : values) {
total = total.add(new BigDecimal(value));
}
return total.doubleValue();
}
public static double subtract(int... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = new BigDecimal(String.valueOf(values[0]));
for (int i = 1; i < values.length; i++) {
total = total.subtract(new BigDecimal(String.valueOf(values[i])));
}
return total.doubleValue();
}
public static double subtract(double... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = new BigDecimal(String.valueOf(values[0]));
for (int i = 1; i < values.length; i++) {
total = total.subtract(new BigDecimal(String.valueOf(values[i])));
}
return total.doubleValue();
}
public static double subtract(String... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = new BigDecimal(values[0]);
for (int i = 1; i < values.length; i++) {
total = total.subtract(new BigDecimal(values[i]));
}
return total.doubleValue();
}
public static double multiply(int... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = new BigDecimal(String.valueOf(values[0]));
for (int i = 1; i < values.length; i++) {
total = total.multiply(new BigDecimal(String.valueOf(values[i])));
}
return total.doubleValue();
}
public static double multiply(double... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = new BigDecimal(String.valueOf(values[0]));
for (int i = 1; i < values.length; i++) {
total = total.multiply(new BigDecimal(String.valueOf(values[i])));
}
return total.doubleValue();
}
public static double multiply(String... values) {
if (values == null || values.length == 0) {
return 0;
}
BigDecimal total = new BigDecimal(values[0]);
for (int i = 1; i < values.length; i++) {
total = total.multiply(new BigDecimal(values[i]));
}
return total.doubleValue();
}
public static double divide(String value1, String value2, int scale, int model) {
return new BigDecimal(value1).divide(new BigDecimal(value2), scale, model).doubleValue();
}
public static double divide(double value1, double value2, int scale, int model) {
return divide(String.valueOf(value1), String.valueOf(value2), scale, model);
}
//指定小数位的最大位数,不四舍五入
public static double divideDown(int value1, int value2, int scale) {
return divide(value1, value2, scale, BigDecimal.ROUND_DOWN);
}
public static double divideDown(double value1, double value2, int scale) {
return divide(value1, value2, scale, BigDecimal.ROUND_DOWN);
}
public static double divideDown(String value1, String value2, int scale) {
return divide(value1, value2, scale, BigDecimal.ROUND_DOWN);
}
//指定小数位的最大位数,四舍五入
public static double divideHalfUp(String value1, String value2, int scale) {
return new BigDecimal(value1).divide(new BigDecimal(value2), scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double divideHalfUp(double value1, double value2, int scale) {
return divideHalfUp(String.valueOf(value1), String.valueOf(value2), scale);
}
public static double abs(int value) {
return abs(String.valueOf(value));
}
public static double abs(double value) {
return abs(String.valueOf(value));
}
public static double abs(String value) {
return new BigDecimal(value).abs().doubleValue();
}
public static double max(int value1, int value2) {
return max(String.valueOf(value1), String.valueOf(value2));
}
public static double max(double value1, double value2) {
return max(String.valueOf(value1), String.valueOf(value2));
}
public static double max(String value1, String value2) {
return new BigDecimal(value1).max(new BigDecimal(value2)).doubleValue();
}
public static double min(int value1, int value2) {
return min(String.valueOf(value1), String.valueOf(value2));
}
public static double min(double value1, double value2) {
return min(String.valueOf(value1), String.valueOf(value2));
}
public static double min(String value1, String value2) {
return new BigDecimal(value1).min(new BigDecimal(value2)).doubleValue();
}
}