JAVA验证数字的整数位长度及小数据位长度
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
public class CommonUtil {
/**
* 验证数字的整数位长度及小数据位长度
* @param str
* @param integerPlaces 整数位数
* @param decimalPlaces 小数位数
* @return
*/
public static boolean checkDoubleFormat(String str,int integerPlaces,int decimalPlaces){
if(str.contains(“E”)){
str = new BigDecimal(str).toPlainString();
}
Pattern p = null;
if (decimalPlaces>0) {
p = Pattern.compile(“1?([0-9]{1,”+integerPlaces+“})+[.]+([0-9]{1,”+decimalPlaces+“})
∣
[
−
+
]
?
[
0
−
9
]
1
,
"
+
i
n
t
e
g
e
r
P
l
a
c
e
s
+
"
|^[-+]?[0-9]{1,"+integerPlaces+"}
∣[−+]?[0−9]1,"+integerPlaces+"”);
}else {
p = Pattern.compile(“2?[0-9]{1,”+integerPlaces+“}$”);
}
Matcher m = p.matcher(str);
boolean flg = m.matches();
return flg;
}
}