Helius
2021-01-06 8eb03554c07de7e268cf230249caf10922c6b47d
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
package com.matrix.system.hive.plugin.util;
 
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Random;
 
import com.matrix.core.tools.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
/**
 * 数字操作类,类型转换等
 * 
 * @author Ron
 * @createTime 2014.08.30
 */
public class NumberUtils {
 
    public static Log log = LogFactory.getLog(NumberUtils.class);
 
    /**
     * 生成0~maxValue的随机数
     * 
     * @param maxValue
     * @return
     */
    public static int random(int maxValue) {
 
        if (maxValue == 0) {
            return 0;
        }
        Random random = new Random();
        if (maxValue < 0) {
            return -(random.nextInt(-maxValue));
        }
        return random.nextInt(maxValue);
    }
    /**
     * 3.1456 -->3.146
    * @Title: getDoubleNum 
    * @author:jyy
    * @param value  值
    * @param num    精度数
    * @return    
    * double    返回类型 
    * @date 2016年8月24日 上午11:03:32 
    * @throws
     */
    public static double getDoubleNum(Double value,int num){
            NumberFormat nFormat=NumberFormat.getNumberInstance(); 
            nFormat.setMaximumFractionDigits(num);//设置小数点后面位数为
            return  new Double(nFormat.format(value).replace(",", ""));
    }
   
    
    /**
     * String转换long,默认值为0
     * 
     * @param str
     * @return
     */
    public static long stringToLong(String str) {
 
        try {
            return Long.valueOf(str).longValue();
        } catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
            return 0;
        }
    }
 
    /**
     * String转换long,默认值为0
     * 
     * @param str
     * @return
     */
    public static int stringToint(String str) {
 
        try {
            return Integer.valueOf(str).intValue();
        } catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
            return 0;
        }
    }
 
    /**
     * 将字符串转换为BidDecaiml类型 str为空返回0
     * 
     * @param str
     * @return
     */
    public static BigDecimal stringToBigDecimal(String str) {
 
        try {
            if (StringUtils.isNotBlank(str)) {
                return new BigDecimal(str);
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
        }
 
        return new BigDecimal(0);
    }
 
    // 检查字符串s是否全为数字
    public static boolean checkIsNumbers(String x) {
 
        if (null == x) {
            return false;
        }
        for (Character c : x.toCharArray()) {
            if (c.compareTo('0') < 0 || c.compareTo('9') > 0) {
                return false;
            }
        }
        return true;
    }
 
    /**
     * 数字1,2,3转成中文数字壹,贰,叁
     * 
     * @param x
     * @return
     */
    public static String NumberTransfromCN(String x) {
 
        String[] unms = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        String[] digits = new String[] { "", "拾", "佰", "仟" };
        String[] units = new String[] { "", "[万]", "[亿]", "[万亿]" };
        return transfrom(x, unms, digits, units);
    }
 
    /**
     * 数字1,2,3转成中文数字一,二,三
     * 
     * @param x
     * @return
     */
    public static String NumberTransfrom(String x) {
 
        String[] unms = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        String[] digits = new String[] { "", "十", "百", "千" };
        String[] units = new String[] { "", "万", "亿", "万亿" };
        return transfrom(x, unms, digits, units);
    }
 
    /**
     * 数字转成中文数字
     * 
     * @param x
     * @return
     */
    public static String transfrom(String x, String[] unms, String[] digits, String[] units) {
 
        if (null == x) {
            return "您输入的字符串地址为null!";
        }
        if (0 == x.length()) {
            return "您输入的字符串长度为0,请输入要转换的数字!";
        }
        if (false == checkIsNumbers(x)) {
            return "您输入的字符不都为数字,无法转换!";
        }
        if (x.length() > 16) {
            return "您输入的字符串长度大于16,无法转换!";
        }
        // 去除字符串首部的0,例如:0010->10
        int fm;
        for (fm = 0; fm < x.length(); fm++) {
            if (x.charAt(fm) != '0') {
                break;
            }
        }
        x = x.substring(fm);// 去除完毕
 
        // 把字符串看作一些组,例如:123456789->1,2345,6789
        String result = "";
        int p = 0;
        int m = x.length() % 4;
        int k = (m > 0 ? x.length() / 4 + 1 : x.length() / 4);
        // 从最左边的那组开始,向右循环
        for (int i = k; i > 0; i--) {
            int len = 4;
            if (i == k && m != 0)// 当i为最左边的那组时,组长度可能有变化
            {
                len = m;
            }
            String s = x.substring(p, p + len);
            int le = s.length();
            for (int j = 0; j < le; j++) {
                int n = Integer.parseInt(s.substring(j, j + 1));
                if (0 == n) {
                    if (j < le - 1 && Integer.parseInt(s.substring(j + 1, j + 2)) > 0 && !result.endsWith(unms[0])) {// 加零的条件:不为最后一位
                        // &&
                        // 下一位数字大于0
                        // &&
                        // 以前没有加过“零”
                        result += unms[0];
                    }
                } else {
                    if (!(n == 1 && (result.endsWith(unms[0]) || result.length() == 0) && j == le - 2)) {// 处理1013一千零"十三",1113
                        // 一千一百"一十三"
                        result += unms[n];
                    }
                    result += digits[le - j - 1];
                }
            }
            if (0 != Integer.parseInt(s))// 如果这组数字不全是 0 ,则加上单位:万,亿,万亿
            {
                result += units[i - 1];
            }
            p += len;
        }
        return result;
    }
}