一些用来做数字验证的正则方法 /** * 是否数字正则表达式 */ public static final String PATTREN_ISNUMERIC = "^(-?\\d+)(\\.\\d+)?$"; /** * 判断是否小数正则表达式 **/ public static final String PARTTENN_ISDECIMAL
/** * 是否数字正则表达式 */ public static final String PATTREN_ISNUMERIC = "^(-?\\d+)(\\.\\d+)?$"; /** * 判断是否小数正则表达式 **/ public static final String PARTTENN_ISDECIMAL = "[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+"; /** * 生成指定的位数编号,不足补零 * * @param businessTypeStr * @param number * @return */ public static String getStringNumber(String businessTypeStr, Long number) { String randomNoStr = String.format("%09d", number); String partyNo = businessTypeStr + randomNoStr; return partyNo; } /** * 生成指定的位数编号,不足补零 * * @param businessTypeStr * @param number * @return */ public static String getStringTwoNumber(String businessTypeStr, Long number) { String randomNoStr = String.format("%02d", number); String partyNo = businessTypeStr + randomNoStr; return partyNo; } public static String getNumWithYYYYMMDD(Long number) { String date = DateUtil.formatDate(new Date(), "yyyyMMdd"); return getStringNumber(date, number); } public static String getBatchNo(Long number) { String date = DateUtil.formatDate(new Date(), "yyyyMMdd"); String randomNoStr = String.format("%05d", number); String partyNo = date + randomNoStr; return partyNo; } /*** * 是否匹配 * @param regex 正则表达式 * @param str 检查字符串 * @return 匹配则返回true, 否则返回false */ private static Boolean isMatch(String regex, String str) { if (StringUtils.isEmpty(str)) { return Boolean.FALSE; } Pattern pattern = Pattern.compile(regex); Matcher isNum = pattern.matcher(str); return isNum.matches(); } /*** * 保留三位小数; * @param val * @return */ public static final String scale(Double val) { if (val == null) { return "0.00%"; } java.text.DecimalFormat df = new java.text.DecimalFormat("##0.00"); return df.format(val * 100) + "%"; } /*** * 判断字符是否数字类型为:小数、正负数等 * @param str * @return Boolean 是数字则返回true,否则返回fase */ public static Boolean isNumeric(String str) { return isMatch(PATTREN_ISNUMERIC, str); } /**** * 判断字符串是否是小数 * @param str 字符串 * @return */ public static Boolean isDecimal(String str) { return isMatch(PARTTENN_ISDECIMAL, str); } /** * 保留三位小数 F: 111,111,11.111 * @param ss * @return */ public static String formatNum(Object ss){ if (ss==null){ return ""; } DecimalFormat df = new DecimalFormat(",###,###.###"); String format = df.format(new BigDecimal(ss.toString())); Float aFloat = Float.valueOf(ss.toString()); if (aFloat<1.0 && aFloat>0){ String s = aFloat * 100+"" ; String format1 = df.format(new BigDecimal(s)); return format1+"%"; } return format; }