public class Test {
public static boolean isNumber(String str) {
Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
Matcher match = pattern.matcher(str);
return match.matches();
}
public static void main(String[] args) {
boolean a = isNumber("1");
boolean b = isNumber("1.0");
boolean c = isNumber("1.22");
boolean d = isNumber("1.222");
boolean e = isNumber("222.22");
boolean f = isNumber("0.01");
boolean g = isNumber("0.001");
boolean h = isNumber("");
boolean i = isNumber("1.");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
System.out.println(i);
Double amountD = Double.parseDouble("1.");
Long fronezAmount = BigDecimal.valueOf(amountD * 100).toBigInteger().longValue();
System.out.println(amountD);
System.out.println(fronezAmount);
}
}
Java 正则校验金额,只能是正整数或2位小数以内
最新推荐文章于 2024-07-26 03:25:22 发布