参考:https://blog.csdn.net/u012402177/article/details/52604725
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);
}
}
测试结果: