chenjiahe
2023-12-04 d9fe4b6ff40a964c895a452ea12a04a13a979ec4
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.math.BigDecimal;
4 import java.math.RoundingMode;
5 import java.text.DecimalFormat;
736fa6 6 import java.util.regex.Matcher;
G 7 import java.util.regex.Pattern;
5c5945 8
E 9 /**
10  * 数字工具
11  * 
12  * @author mgchen
13  *
14  */
15 public class NumberUtil {
16
17     private static DecimalFormat fmt = new DecimalFormat("0.00");
18
19     private static DecimalFormat fmtOne = new DecimalFormat("0.0");
20
21     private static DecimalFormat fmtThree = new DecimalFormat("0.000");
22
23     /**
24      * 获取指定格式的Double 类型数据
25      * @param e
26      *            要转换的数据源
27      **/
28     public static Double getTypeDouble(Double e) {
29         fmt.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确
30         Double d = Double.valueOf(fmt.format(e));
31         return d;
32     }
33
34     /**
35      * 获取指定格式的Double 类型数据
36      *
37      * @param e
38      *            要转换的数据源
39      **/
40     public static Double getTypeDouble1(Double e) {
41         fmtOne.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确
42         Double d = Double.valueOf(fmtOne.format(e));
43         return d;
44     }
45
46     public static Double getTypeDouble2(Double e) {
47         fmtThree.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确
48         Double d = Double.valueOf(fmtThree.format(e));
49         return d;
50     }
51
52     /**
53      * 把存储的分字符串转成元的浮点数 例如100到1.0
54      */
55     public static Double getYunFromFenStr(String fenStr) {
56         return getTypeDouble(Integer.parseInt(fenStr) * 1.0 / 100);
57     }
58
59     /**
60      * 把存储的分转成元的浮点数 例如100到1.0
61      */
62     public static Double getYunFromFen(Integer fen) {
63         if (fen == null) {
64             return 0D;
65         }
66         return getTypeDouble(fen * 1.0 / 100);
67     }
68
69     /**
70      * 把存储的厘转成元的浮点数 例如1000到1.0
71      */
72     public static Double getYunFromLi(int li) {
73         return getTypeDouble2(li * 1.0 / 1000);
74     }
75
76     /**
77      * 把存储的数值转为百分比
78      * 
79      * @param rate
80      * @return
81      */
82     public static Double getRateFromInt(int rate) {
83         return getTypeDouble(rate * 1.0 / 100);
84     }
85
86     /** 浮点数四舍五入取整 */
87     public static int getRoundHalfUp(double tar) {
88         return new BigDecimal(tar).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
89     }
90
91     /** 浮点数四舍五入取整 */
92     public static int getRoundHalfDown(double tar) {
93         Double d = Math.floor(tar);
94         return d.intValue();
95     }
96
97     /**
98      * 把一个double的字符串乘100 返回整数
99      *
100      * @return int 整数
101      */
102     public static int getYunFromFen(String str) {
103         // BigDecimal b1 = new BigDecimal(str);
104         // BigDecimal b2 = new BigDecimal("100");
105         // return b1.multiply(b2).intValue();
106         return multiply(str, "100").intValue();
107     }
108
109     /** 获取两位小数的整数 */
110     public static int getTwoDecimalInt(Double d) {
111         return Double.valueOf(fmt.format(d)).intValue();
112     }
113
114     /**
115      * num1 除以num2 得到百分百 保留一位小数
116      * 
117      * @param num1
118      * @param num2
119      * @return
120      */
121     public static Double getPercentage(Integer num1, Integer num2) {
122         Double reNum = 0d;
123         if (num1 == null || num2 == null || num1 == 0 || num2 == 0) {
124             return reNum;
125         }
126         BigDecimal b1 = new BigDecimal(num1 * 100);
127         BigDecimal b2 = new BigDecimal(num2);
128         reNum = b1.divide(b2, 1, BigDecimal.ROUND_HALF_UP).doubleValue();
129         return reNum;
130     }
131
132     /**
133      * 将一个数除以100
134      * 
135      * @param fen
136      * @return Double
137      */
138     public static Double fenForYuan(int fen) {
139         // BigDecimal b1 = new BigDecimal(fen);
140         // BigDecimal b2 = new BigDecimal(100);
141         // return b1.divide(b2).doubleValue();
142         return divide(fen, 100);
143     }
144
145     /**
146      * 将一个数除以100
147      * 
148      * @param fen
149      * @return Double
150      */
151     public static Double fenForYuan(String fen) {
152         // BigDecimal b1 = new BigDecimal(fen);
153         // BigDecimal b2 = new BigDecimal(100);
154         // return b1.divide(b2).doubleValue();
155         return divide(fen, "100");
156     }
157
158     public static void main(String[] args) {
159         double d = 299999d;
160         System.out.println(getTwoOfDouble(d));
161     }
162
163     /**
164      * 两个数相除
165      * 
166      * @param num1
167      *            数字1
168      * @param num2
169      *            数字2
170      * @return
171      */
172     public static Double divide(int num1, int num2) {
173         return divide(num1 * 1D, num2 * 1D);
174     }
175
176     /**
177      * 两个数相除- 保留两位小数点
178      * 
179      * @param num1
180      *            数字1
181      * @param num2
182      *            数字2
183      * @return
184      */
185     public static Double divide(String num1, String num2) {
186         return divide(Double.parseDouble(num1), Double.parseDouble(num1), 2);
187     }
188
189     /**
190      * 两个数相除- 保留两位小数点
191      * 
192      * @param num1
193      *            数字1
194      * @param num2
195      *            数字2
196      * @return
197      */
198     public static Double divide(Double num1, Double num2) {
199         return divide(num1, num2, 2);
200     }
201
202     /**
203      * 两个数相除
204      * 
205      * @param num1
206      *            数字1
207      * @param num2
208      *            数字2
209      * @param scale
210      *            小数位数, 默认保留2位
211      * @return
212      */
213     public static Double divide(Double num1, Double num2, Integer scale) {
214         if (scale == null || scale < 0) {
215             scale = 2;
216         }
217         BigDecimal b1 = new BigDecimal(num1);
218         BigDecimal b2 = new BigDecimal(num2);
219         return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
220     }
221
222     /**
223      * 
224      * 两个数相除
225      *
226      * @param num1
227      *            数字1
228      * @param num2
229      *            数字2
230      * @param scale
231      *            小数位数, 默认保留2位
232      * @return
233      */
234     public static Double divideDown(Double num1, Double num2, Integer scale) {
235         if (scale == null || scale < 0) {
236             scale = 2;
237         }
238         BigDecimal b1 = new BigDecimal(num1);
239         BigDecimal b2 = new BigDecimal(num2);
240
241         return b1.divide(b2, scale, BigDecimal.ROUND_DOWN).doubleValue();
242     }
243
244     /**
245      * 两个Double数相乘
246      * 
247      * @param num1
248      * @param num2
249      * @return Double
250      * 
251      */
252     public static Double multiply(Double num1, Double num2) {
253         return multiply(num1.toString(), num2.toString());
254     }
255
256     /**
257      * 两个Double数相乘
258      * 
259      * @param num1
260      * @param num2
261      * @return Double
262      * 
263      */
264     public static Double multiply(String num1, String num2) {
265         BigDecimal b1 = new BigDecimal(num1);
266         BigDecimal b2 = new BigDecimal(num2);
267         return b1.multiply(b2).doubleValue();
268     }
269
270     /**
271      * 两个Double数相减
272      * 
273      * @param v1
274      * @param v2
275      * @return Double
276      */
277     public static Double subtract(Double v1, Double v2) {
278         return subtract(v2.toString(), v2.toString());
279     }
280
281     /**
282      * 两个Double数相减
283      * 
284      * @param v1
285      * @param v2
286      * @return Double
287      */
288     public static Double subtract(String v1, String v2) {
289         BigDecimal b1 = new BigDecimal(v1);
290         BigDecimal b2 = new BigDecimal(v2);
291         return b1.subtract(b2).doubleValue();
292     }
293
294     /**
295      * 两个Double数相加
296      * 
297      * @param v1
298      * @param v2
299      * @return Double
300      */
301     public static Double add(Double v1, Double v2) {
302         return add(v1.toString(), v2.toString());
303     }
304
305     /**
306      * 两个Double数相加
307      * 
308      * @param v1
309      * @param v2
310      * @return Double
311      */
312     public static Double add(String v1, String v2) {
313         BigDecimal b1 = new BigDecimal(v1);
314         BigDecimal b2 = new BigDecimal(v2);
315         return b1.add(b2).doubleValue();
316     }
317
318     /** 从元转化为分,元为2位小数 */
319     public static int getFenFromYun(String doubleStr) {
320         return getRoundHalfUp(Double.parseDouble(doubleStr) * 100);
321     }
322
323     /**获取两位小数的字符串*/
324     public static String getTwoOfDouble(double value)
325     {
326         String str = value + "";
327
328         //如果有小数点,则两位小数
329         int pointIndex = str.indexOf(".");
330         if(pointIndex != -1)
331         {
332             int len = str.length();
333             if(len - (pointIndex + 1) > 2)
334             {
335                 return str.substring(0, pointIndex + 3);
336             }
337         }
338
339         return str;
340     }
341
e27fdf 342     /**Double类型转化
C 343      * @param data 数据
344      * @param digitNumber 保留多少位小数
345      * @return
346      */
347     public static Double getTypeDouble(Double data,Integer digitNumber) {
348         //利用BigDecimal来实现四舍五入.保留一位小数
349         double result2 = new BigDecimal(data).setScale(digitNumber, BigDecimal.ROUND_HALF_UP).doubleValue();
350         //1代表保留1位小数,保留两位小数就是2
351         //BigDecimal.ROUND_HALF_UP 代表使用四舍五入的方式
352
353         return result2;
354     }
355
736fa6 356     /**
G 357      * 从字符串中提取小数
358      * @param str
359      * @return
360      */
361     public static String getNumber(String str){
362         // 控制正则表达式的匹配行为的参数(小数)
363         Pattern p = Pattern.compile("(\\d+\\.\\d+)");
364         //Matcher类的构造方法也是私有的,不能随意创建,只能通过Pattern.matcher(CharSequence input)方法得到该类的实例.
365         Matcher m = p.matcher(str);
366         //m.find用来判断该字符串中是否含有与"(\\d+\\.\\d+)"相匹配的子串
367         if (m.find()) {
368             //如果有相匹配的,则判断是否为null操作
369             //group()中的参数:0表示匹配整个正则,1表示匹配第一个括号的正则,2表示匹配第二个正则,在这只有一个括号,即1和0是一样的
370             str = m.group(1) == null ? "" : m.group(1);
371         } else {
372             //如果匹配不到小数,就进行整数匹配
373             p = Pattern.compile("(\\d+)");
374             m = p.matcher(str);
375             if (m.find()) {
376                 //如果有整数相匹配
377                 str = m.group(1) == null ? "" : m.group(1);
378             } else {
379                 //如果没有小数和整数相匹配,即字符串中没有整数和小数,就设为空
380                 str = "";
381             }
382         }
383         return str;
384     }
385
d182dc 386     /** 校验字符串是否整数 */
F 387     public static boolean checkStrIsInt(String str){
388         if(StringUtils.isEmpty(str)){
389             return false;
390         }
391         try{
392             Integer.parseInt(str);
393             return true;
394         }catch (Exception e){
395             return false;
396         }
397     }
398
399     /**
400      * 字符串转整形
401      * @param str
402      * @param defInt    默认整数
403      * @return
404      */
405     public static int strChangeInt(String str, Integer defInt){
406         if(defInt == null){
407             defInt = 0;
408         }
409         if(StringUtils.isEmpty(str)){
410             return defInt;
411         }
412         try{
413             return Integer.parseInt(str);
414         }catch (Exception e){
415             return defInt;
416         }
417     }
5c5945 418 }