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
|
package
com.hanchao.test;
import
org.apache.commons.lang.math.NumberUtils;
/**
* @author liweihan (liweihan@sohu-inc.com)
* @version 1.0 (2016年1月14日 下午2:11:34)
*/
public
class
NumberUtilsTest {
public
static
void
main(String[] args) {
/**
* 1.NumberUtils.isNumber():判断字符串是否是数字
* */
System.out.println(NumberUtils.isNumber(
"5.96"
));
//true
System.out.println(NumberUtils.isNumber(
"s5"
));
//false
System.out.println(NumberUtils.isNumber(
"0000234"
));
//true
/**
* 2..NumberUtils.isDigits():判断字符串中是否全为数字
*/
System.out.println(NumberUtils.isDigits(
"0.2348"
));
//false
System.out.println(NumberUtils.isDigits(
"000000.2348"
));
//false
System.out.println(NumberUtils.isDigits(
"0000234"
));
//true
/**
* 3..NumberUtils.toInt():字符串转换为整数
*/
System.out.println(NumberUtils.toDouble(
"6"
));
//6.0
System.out.println(NumberUtils.toDouble(
""
, 8d));
//8.0
System.out.println(NumberUtils.toDouble(
"s"
, 9d));
//9.0
System.out.println(NumberUtils.toInt(
"2"
));
//2
System.out.println(NumberUtils.toInt(
""
,
1
));
//1
System.out.println(NumberUtils.toInt(
"noInt"
,
3
));
//3
System.out.println(NumberUtils.toFloat(
"3"
));
//3.0
System.out.println(NumberUtils.toFloat(
""
, 4f));
//4.0
System.out.println(NumberUtils.toFloat(
"no"
,4f));
//4.0
System.out.println(NumberUtils.toLong(
"5"
));
//5
System.out.println(NumberUtils.toLong(
""
,5L));
//5
System.out.println(NumberUtils.toLong(
"ss"
, 5l));
//5
/**
* 4.NumberUtils.max():找出最大的一个
*/
System.out.println(NumberUtils.max(
new
int
[]{
3
,
5
,
7
,
9
}));
//9
System.out.println(NumberUtils.max(
new
float
[]{2f,3f,5f,6f}));
//6.0
System.out.println(NumberUtils.max(3d,1d,7d));
//7.0
/**
* 5.NumberUtils.min():找出最小的一个
*/
System.out.println(NumberUtils.min(
new
int
[]{
3
,
5
,
7
,
9
}));
//3
System.out.println(NumberUtils.min(
new
float
[]{2f,3f,5f,6f}));
//2.0
System.out.println(NumberUtils.min(3d,1d,7d));
//1.0
/**
* 6.NumberUtils.createBigDecimal()通过字符串创建BigDecimal类型,支持long、int、float、double、number等数值
*/
System.out.println(NumberUtils.createBigDecimal(
"1"
));
//1
System.out.println(NumberUtils.createDouble(
"1"
));
//1.0
System.out.println(NumberUtils.createLong(
"1"
));
//1
System.out.println(NumberUtils.createBigInteger(
"1"
));
//1
System.out.println(NumberUtils.createFloat(
"1"
));
//1.0
}
}
|
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/1735023
,如需转载请自行联系原作者