public static String formatWithString(double value, int digit) { String format = String.format("%%.%df", digit); return String.format(format, value).toString(); } public static String formatWithFormatter(double value, int digit) { String format = String.format("%%.%df", digit); return new Formatter().format(format, value).toString(); } public static String formatWithBigDecimal(double value, int digit) { BigDecimal bd = new BigDecimal(value); bd = bd.setScale(digit, RoundingMode.HALF_UP); return bd.toString(); } public static String formatWithDecimalFormat(double src, int digit) { String pre_format = String.format(".%%0%dd", digit); String format = String.format(pre_format, 0); DecimalFormat df = new java.text.DecimalFormat(format); //".00" df.setRoundingMode(RoundingMode.HALF_UP); String value = df.format(src); return value; } public static String formatWithNumberFormat(double value, int digit) { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(digit); nf.setMinimumFractionDigits(digit); nf.setRoundingMode(RoundingMode.HALF_UP); // 如果想输出的格式用逗号隔开,可以设置成true nf.setGroupingUsed(false); return nf.format(value); }
public static boolean isPhoneByPattern(String phone) { String regex = "^1[3|4|5|8]\\d{9}$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(phone); return matcher.matches(); } public static boolean isPhoneByString(String phone) { // "[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。 String regex = "[1][358]\\d{9}"; return phone.matches(regex); } public static boolean isEmailByPattern(String email) { String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); return matcher.matches(); } public static boolean isEmailByString(String email) { String regex = "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)"; return email.matches(regex); } public static boolean isICNOByPattern(String icno) { String regex15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$"; Pattern pattern15 = Pattern.compile(regex15); Matcher matcher15 = pattern15.matcher(icno); String regex18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x|X)$"; Pattern pattern18 = Pattern.compile(regex18); Matcher matcher18 = pattern18.matcher(icno); return (matcher15.matches() || matcher18.matches()); } public static boolean isICNOByString(String icno) { String regex15 = "[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}"; String regex18 = "[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x|X)"; return (icno.matches(regex15) || icno.matches(regex18)); }
点击这里下载计算器需要的运算源码