正则表达式相关知识
https://www.runoob.com/java/java-regular-expressions.html
方法1
private static final Pattern INT_DECIMAL2 = Pattern.compile("^((\\d+)|(0.\\d{1,2})|([1-9]\\d*.\\d{1,2}))$");
/**
* 正整数或者正浮点类型数据最大保留两位
* @param req
* @return
*/
public static boolean isIntOrDecimal2(String req){
Matcher matcher=INT_DECIMAL2.matcher(req);
return matcher.matches();
}
方法2
int POITLEN=2
int poitIndex=amountStr.indexOf(".");
if(poitIndex>=0){
poitIndex=amountStr.substring(poitIndex+1).length();
}
if(req.getAmount()<0||poitIndex>POITLEN){
throw new Exception("小于0或小数位大于2位");
}