chenjiahe
2022-07-12 cd5e48b7aa68c555e77402832bc84e87a47853ad
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.math.BigDecimal;
4 import java.math.RoundingMode;
5 import java.text.DecimalFormat;
6
7 /**
8  * 数字工具
9  * 
10  * @author mgchen
11  *
12  */
13 public class NumberUtil {
14
15     private static DecimalFormat fmt = new DecimalFormat("0.00");
16
17     private static DecimalFormat fmtOne = new DecimalFormat("0.0");
18
19     private static DecimalFormat fmtThree = new DecimalFormat("0.000");
20
21     /**
22      * 获取指定格式的Double 类型数据
23      * @param e
24      *            要转换的数据源
25      **/
26     public static Double getTypeDouble(Double e) {
27         fmt.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确
28         Double d = Double.valueOf(fmt.format(e));
29         return d;
30     }
31
32     /**
33      * 获取指定格式的Double 类型数据
34      *
35      * @param e
36      *            要转换的数据源
37      **/
38     public static Double getTypeDouble1(Double e) {
39         fmtOne.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确
40         Double d = Double.valueOf(fmtOne.format(e));
41         return d;
42     }
43
44     public static Double getTypeDouble2(Double e) {
45         fmtThree.setRoundingMode(RoundingMode.HALF_UP); // 保留小数点后两位并四舍五入,确保价钱准确
46         Double d = Double.valueOf(fmtThree.format(e));
47         return d;
48     }
49
50     /**
51      * 把存储的分字符串转成元的浮点数 例如100到1.0
52      */
53     public static Double getYunFromFenStr(String fenStr) {
54         return getTypeDouble(Integer.parseInt(fenStr) * 1.0 / 100);
55     }
56
57     /**
58      * 把存储的分转成元的浮点数 例如100到1.0
59      */
60     public static Double getYunFromFen(Integer fen) {
61         if (fen == null) {
62             return 0D;
63         }
64         return getTypeDouble(fen * 1.0 / 100);
65     }
66
67     /**
68      * 把存储的厘转成元的浮点数 例如1000到1.0
69      */
70     public static Double getYunFromLi(int li) {
71         return getTypeDouble2(li * 1.0 / 1000);
72     }
73
74     /**
75      * 把存储的数值转为百分比
76      * 
77      * @param rate
78      * @return
79      */
80     public static Double getRateFromInt(int rate) {
81         return getTypeDouble(rate * 1.0 / 100);
82     }
83
84     /** 浮点数四舍五入取整 */
85     public static int getRoundHalfUp(double tar) {
86         return new BigDecimal(tar).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
87     }
88
89     /** 浮点数四舍五入取整 */
90     public static int getRoundHalfDown(double tar) {
91         Double d = Math.floor(tar);
92         return d.intValue();
93     }
94
95     /**
96      * 把一个double的字符串乘100 返回整数
97      *
98      * @return int 整数
99      */
100     public static int getYunFromFen(String str) {
101         // BigDecimal b1 = new BigDecimal(str);
102         // BigDecimal b2 = new BigDecimal("100");
103         // return b1.multiply(b2).intValue();
104         return multiply(str, "100").intValue();
105     }
106
107     /** 获取两位小数的整数 */
108     public static int getTwoDecimalInt(Double d) {
109         return Double.valueOf(fmt.format(d)).intValue();
110     }
111
112     /**
113      * num1 除以num2 得到百分百 保留一位小数
114      * 
115      * @param num1
116      * @param num2
117      * @return
118      */
119     public static Double getPercentage(Integer num1, Integer num2) {
120         Double reNum = 0d;
121         if (num1 == null || num2 == null || num1 == 0 || num2 == 0) {
122             return reNum;
123         }
124         BigDecimal b1 = new BigDecimal(num1 * 100);
125         BigDecimal b2 = new BigDecimal(num2);
126         reNum = b1.divide(b2, 1, BigDecimal.ROUND_HALF_UP).doubleValue();
127         return reNum;
128     }
129
130     /**
131      * 将一个数除以100
132      * 
133      * @param fen
134      * @return Double
135      */
136     public static Double fenForYuan(int fen) {
137         // BigDecimal b1 = new BigDecimal(fen);
138         // BigDecimal b2 = new BigDecimal(100);
139         // return b1.divide(b2).doubleValue();
140         return divide(fen, 100);
141     }
142
143     /**
144      * 将一个数除以100
145      * 
146      * @param fen
147      * @return Double
148      */
149     public static Double fenForYuan(String fen) {
150         // BigDecimal b1 = new BigDecimal(fen);
151         // BigDecimal b2 = new BigDecimal(100);
152         // return b1.divide(b2).doubleValue();
153         return divide(fen, "100");
154     }
155
156     public static void main(String[] args) {
157         double d = 299999d;
158         System.out.println(getTwoOfDouble(d));
159     }
160
161     /**
162      * 两个数相除
163      * 
164      * @param num1
165      *            数字1
166      * @param num2
167      *            数字2
168      * @return
169      */
170     public static Double divide(int num1, int num2) {
171         return divide(num1 * 1D, num2 * 1D);
172     }
173
174     /**
175      * 两个数相除- 保留两位小数点
176      * 
177      * @param num1
178      *            数字1
179      * @param num2
180      *            数字2
181      * @return
182      */
183     public static Double divide(String num1, String num2) {
184         return divide(Double.parseDouble(num1), Double.parseDouble(num1), 2);
185     }
186
187     /**
188      * 两个数相除- 保留两位小数点
189      * 
190      * @param num1
191      *            数字1
192      * @param num2
193      *            数字2
194      * @return
195      */
196     public static Double divide(Double num1, Double num2) {
197         return divide(num1, num2, 2);
198     }
199
200     /**
201      * 两个数相除
202      * 
203      * @param num1
204      *            数字1
205      * @param num2
206      *            数字2
207      * @param scale
208      *            小数位数, 默认保留2位
209      * @return
210      */
211     public static Double divide(Double num1, Double num2, Integer scale) {
212         if (scale == null || scale < 0) {
213             scale = 2;
214         }
215         BigDecimal b1 = new BigDecimal(num1);
216         BigDecimal b2 = new BigDecimal(num2);
217         return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
218     }
219
220     /**
221      * 
222      * 两个数相除
223      *
224      * @param num1
225      *            数字1
226      * @param num2
227      *            数字2
228      * @param scale
229      *            小数位数, 默认保留2位
230      * @return
231      */
232     public static Double divideDown(Double num1, Double num2, Integer scale) {
233         if (scale == null || scale < 0) {
234             scale = 2;
235         }
236         BigDecimal b1 = new BigDecimal(num1);
237         BigDecimal b2 = new BigDecimal(num2);
238
239         return b1.divide(b2, scale, BigDecimal.ROUND_DOWN).doubleValue();
240     }
241
242     /**
243      * 两个Double数相乘
244      * 
245      * @param num1
246      * @param num2
247      * @return Double
248      * 
249      */
250     public static Double multiply(Double num1, Double num2) {
251         return multiply(num1.toString(), num2.toString());
252     }
253
254     /**
255      * 两个Double数相乘
256      * 
257      * @param num1
258      * @param num2
259      * @return Double
260      * 
261      */
262     public static Double multiply(String num1, String num2) {
263         BigDecimal b1 = new BigDecimal(num1);
264         BigDecimal b2 = new BigDecimal(num2);
265         return b1.multiply(b2).doubleValue();
266     }
267
268     /**
269      * 两个Double数相减
270      * 
271      * @param v1
272      * @param v2
273      * @return Double
274      */
275     public static Double subtract(Double v1, Double v2) {
276         return subtract(v2.toString(), v2.toString());
277     }
278
279     /**
280      * 两个Double数相减
281      * 
282      * @param v1
283      * @param v2
284      * @return Double
285      */
286     public static Double subtract(String v1, String v2) {
287         BigDecimal b1 = new BigDecimal(v1);
288         BigDecimal b2 = new BigDecimal(v2);
289         return b1.subtract(b2).doubleValue();
290     }
291
292     /**
293      * 两个Double数相加
294      * 
295      * @param v1
296      * @param v2
297      * @return Double
298      */
299     public static Double add(Double v1, Double v2) {
300         return add(v1.toString(), v2.toString());
301     }
302
303     /**
304      * 两个Double数相加
305      * 
306      * @param v1
307      * @param v2
308      * @return Double
309      */
310     public static Double add(String v1, String v2) {
311         BigDecimal b1 = new BigDecimal(v1);
312         BigDecimal b2 = new BigDecimal(v2);
313         return b1.add(b2).doubleValue();
314     }
315
316     /** 从元转化为分,元为2位小数 */
317     public static int getFenFromYun(String doubleStr) {
318         return getRoundHalfUp(Double.parseDouble(doubleStr) * 100);
319     }
320
321     /**获取两位小数的字符串*/
322     public static String getTwoOfDouble(double value)
323     {
324         String str = value + "";
325
326         //如果有小数点,则两位小数
327         int pointIndex = str.indexOf(".");
328         if(pointIndex != -1)
329         {
330             int len = str.length();
331             if(len - (pointIndex + 1) > 2)
332             {
333                 return str.substring(0, pointIndex + 3);
334             }
335         }
336
337         return str;
338     }
339
e27fdf 340     /**Double类型转化
C 341      * @param data 数据
342      * @param digitNumber 保留多少位小数
343      * @return
344      */
345     public static Double getTypeDouble(Double data,Integer digitNumber) {
346         //利用BigDecimal来实现四舍五入.保留一位小数
347         double result2 = new BigDecimal(data).setScale(digitNumber, BigDecimal.ROUND_HALF_UP).doubleValue();
348         //1代表保留1位小数,保留两位小数就是2
349         //BigDecimal.ROUND_HALF_UP 代表使用四舍五入的方式
350
351         return result2;
352     }
353
5c5945 354 }