标签:contains form 表达式 cal equal sub hal system format
/** * 格式化字符串 */ static String dataFormat(String data){ String formatedData = ""; // 浮点数正则表达式 Pattern pattern = Pattern.compile("^(-?\\d+)(\\.\\d+)?$"); if(data == null) return null; else { if(data.equals("")) formatedData = ""; else if(!data.contains(",")){ formatedData += new BigDecimal(data).setScale(2, BigDecimal.ROUND_HALF_UP); } else{ while(data.length() != 0){ int position = data.indexOf(","); if(data.indexOf(",") != -1){ String tempString = data.substring(0, position); if(pattern.matcher(tempString).matches() ){ formatedData += new BigDecimal(tempString).setScale(2, BigDecimal.ROUND_HALF_UP); formatedData += ","; data = data.substring(position + 1); }else if (tempString.equals("")) { formatedData += ","; data = data.substring(position + 1); }else{ System.out.println("数据格式不正确"); return ""; } } } } } return formatedData; }
格式化数据保留两位小数,输入格式为 :xxx,xx,,,,x,,(x为浮点数)
标签:contains form 表达式 cal equal sub hal system format
原文地址:https://www.cnblogs.com/TheKiteRunner/p/8966522.html