判断字符串是否为数字

/**
* 匹配是否为数字
* @param str 可能为中文,也可能是-19162431.1254,不使用BigDecimal的话,变成-1.91624311254E7
* @return
* @author yutao <<<<<<<<<<<<<<<<<<<<<<<<<作者
* @date 2016年11月14日下午7:41:22
*/
public static boolean isNumeric(String str) {
// 该正则表达式可以匹配所有的数字 包括负数
Pattern pattern = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
String bigStr;
try {
bigStr = new BigDecimal(str).toString();
} catch (Exception e) {
return false;
}

Matcher isNum = pattern.matcher(bigStr); // matcher是全匹配
if (!isNum.matches()) {
return false;
}
return true;
}

posted @ 2019-12-23 17:00  阿灿1024  阅读(648)  评论(0)    收藏  举报