判斷字符串是否為數字


/**
* 匹配是否為數字
* @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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM