java对BigDecimal数字位数验证
/**
* time: 2023年5月4日
* str:验证数值
* integerPlaces:小数点前位数
* decimalPlaces:小数点后位数
*/
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("^[-+]?([0-9]{1,"+integerPlaces+"})+[.]+([0-9] {1,"+decimalPlaces+"})$|^[-+]?[0-9]{1,"+integerPlaces+"}$");
}else {
p = Pattern.compile("^[-+]?[0-9]{1,"+integerPlaces+"}$");
}
//小数点后位数的验证
if (decimalPlaces>0) {
p = Pattern.compile("^[-+]?([0-9]{1,"+1+"})+[.]+([0-9]{1,"+decimalPlaces+"})$");
Matcher m = p.matcher(str);
flg = m.matches();
}
Matcher m = p.matcher(str);
boolean flg = m.matches();
return flg;
}