NumberUtils.java
1.4 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
package com.diligrp.assistant.shared.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 字符串-数值转化工具类
*/
public class NumberUtils {
private static Logger LOG = LoggerFactory.getLogger(NumberUtils.class);
public static int str2Int(String number, int defaultValue) {
if (ObjectUtils.isEmpty(number)) {
return defaultValue;
}
try {
return Integer.parseInt(number);
} catch (NumberFormatException nfe) {
// Never ignore any exception
LOG.error("Invalid number format", nfe);
return defaultValue;
}
}
public static long str2Long(String number, long defaultValue) {
if (ObjectUtils.isEmpty(number)) {
return defaultValue;
}
try {
return Long.parseLong(number);
} catch (NumberFormatException nfe) {
// Never ignore any exception
LOG.error("Invalid number format", nfe);
return defaultValue;
}
}
public static boolean isNumeric(String str) {
if (ObjectUtils.isEmpty(str)) {
return false;
} else {
int sz = str.length();
for(int i = 0; i < sz; ++i) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
}
}