2
peternameyakj
2024-08-14 9bcf85f8d8f9b503e386a67924f5943cd77bd813
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package kernel.util;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.springframework.util.Assert;
 
/**
 * 提供高精度的运算支持.
 * 所以函数以double为参数类型,兼容int与float.
 * 
 */
public abstract class NumberUtils extends org.springframework.util.NumberUtils {
    /**
     * 精确的加法运算.
     */
    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.add(b2).doubleValue();
    }
 
    /**
     * 
     * 精确的减法运算.
     * 
     * @param v1 被减数
     * @param v2 减数
     */
    public static double subtract(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }
 
    /**
     * 提供精确的乘法运算.
     */
    public static double multiply(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).doubleValue();
    }
 
    /**
     * 提供精确的乘法运算,并对运算结果截位.
     * 
     * @param scale 运算结果小数后精确的位数
     */
    public static double multiply(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
 
    /**
     * 提供(相对)精确的除法运算.
     * 
     * @see #divide(double, double, int)
     */
    public static double divide(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.divide(b2).doubleValue();
    }
 
    /**
     * 提供(相对)精确的除法运算.
     * 由scale参数指定精度,以后的数字四舍五入.
     * 
     * @param v1 被除数
     * @param v2 除数
     * @param scale 表示表示需要精确到小数点以后几位
     */
    public static double divide(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
 
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
 
    /**
     * 提供精确的小数位四舍五入处理.
     * 
     * @param v 需要四舍五入的数字
     * @param scale 小数点后保留几位
     */
    public static double round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(v);
        return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
 
    public static int compare(Number left, Number right) throws IllegalArgumentException {
        Assert.notNull(left, "left number must not be null");
        Assert.notNull(right, "right number must not be null");
 
        if (left.getClass().equals(Byte.class)) {
            return ((Byte) left).compareTo((Byte) right);
        }
        else if (left.getClass().equals(Short.class)) {
            return ((Short) left).compareTo((Short) right);
        }
        else if (left.getClass().equals(Integer.class)) {
            return ((Integer) left).compareTo((Integer) right);
        }
        else if (left.getClass().equals(Long.class)) {
            return ((Long) left).compareTo((Long) right);
        }
        else if (left.getClass().equals(BigInteger.class)) {
            return ((BigInteger) left).compareTo((BigInteger) right);
        }
        else if (left.getClass().equals(Float.class)) {
            return ((Float) left).compareTo((Float) right);
        }
        else if (left.getClass().equals(Double.class)) {
            return ((Double) left).compareTo((Double) right);
        }
        else if (left.getClass().equals(BigDecimal.class)) {
            return ((BigDecimal) left).compareTo((BigDecimal) right);
        }
        else {
            throw new IllegalArgumentException("Could not compare left number [" + left + "] of type ["
                    + left.getClass().getName() + "] to right number [" + right + "] of type ["
                    + right.getClass().getName() + "]");
        }
    }
 
    public static int safeLongToInt(long l) {
        if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
        }
        return (int) l;
    }
 
    /**
     * �����ָ�ʽ��Ϊ��Ǯ��ʽ
     * 
     * @param number
     *            ���ʽ����BigDecimal
     * @return С����2λ��ʽ���ַ�
     */
    public static String formatNumericFee(BigDecimal number) {
        return number.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
 
    /**
     * �����ָ�ʽ��Ϊ����ʽ
     * 
     * @param number
     *            ���ʽ����BigDecimal
     * @return С����6λ��ʽ���ַ�
     */
    public static String formatNumericTraffic(BigDecimal number) {
        return number.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
    }
 
    // ���һ��ת����������bigdecimalת��ΪTB��GB��MB����ʽ
    public static String mbToTGM(BigDecimal mb) {
        StringBuffer sb = new StringBuffer(200);
        int g = 0;
        int t = 0;
        BigDecimal m = new BigDecimal(0);
 
        t = new Float(mb.divide(new BigDecimal(1024 * 1024), BigDecimal.ROUND_DOWN).floatValue()).intValue();
        mb = mb.subtract((new BigDecimal(t).multiply(new BigDecimal(1024 * 1024))));
        g = new Float(mb.divide(new BigDecimal(1024), BigDecimal.ROUND_DOWN).floatValue()).intValue();
        mb = mb.subtract((new BigDecimal(g).multiply(new BigDecimal(1024))));
        m = mb.setScale(2, BigDecimal.ROUND_HALF_UP);
 
        sb.append(new Integer(t).toString()).append(" TB ");
 
        sb.append(new Integer(g).toString()).append(" GB ");
 
        sb.append(m.toString()).append(" MB ");
 
        return sb.toString();
    }
 
    // ��value��ǰ4���ֽڶs����һ�������
    public static Integer readInt(byte[] value) {
        if (value == null || value.length < 4) {
            return null;
        }
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(value);
            DataInputStream dis = new DataInputStream(bais);
            Integer retval = dis.readInt();
            bais.close();
            dis.close();
            return retval;
        } catch (Exception e) {
            return null;
        }
    }
 
    // ��value��ǰ}���ֽڶs����һ�������
    public static Short readShort(byte[] value) {
        if (value == null || value.length < 2) {
            return null;
        }
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(value);
            DataInputStream dis = new DataInputStream(bais);
            Short retval = dis.readShort();
            bais.close();
            dis.close();
            return retval;
        } catch (Exception e) {
            return null;
        }
    }
 
    public static byte[] intToByteArray(Integer value) {
        if (value == null) {
            return null;
        }
 
        try {
            byte[] data = null;
            ByteArrayOutputStream bais = new ByteArrayOutputStream(4);
            DataOutputStream dos = new DataOutputStream(bais);
            dos.writeInt(value);
            data = bais.toByteArray();
            bais.close();
            dos.close();
            return data;
        } catch (Exception e) {
            return null;
        }
    }
 
    public static byte[] longToByteArray(long value) {
        try {
            byte[] data = null;
            ByteArrayOutputStream bais = new ByteArrayOutputStream(8);
            DataOutputStream dos = new DataOutputStream(bais);
            dos.writeLong(value);
            data = bais.toByteArray();
            bais.close();
            dos.close();
            return data;
        } catch (Exception e) {
            return null;
        }
    }
 
    public static Long readUnsingedInt(byte[] data, int offset) {
        try {
            DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
            dis.skip(offset);
            Long retval = null;
            int tmp = dis.readInt();
            if (tmp < 0) {
                retval = tmp + 4294967296L;
            }
            else {
                retval = Long.valueOf(tmp);
            }
 
            dis.close();
            return retval;
        } catch (Exception e) {
            return null;
        }
    }
 
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}