/**
* 格式化字符串
*/
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(",")){
try {
formatedData += new BigDecimal(data).setScale(2, BigDecimal.ROUND_HALF_UP);
} catch (Exception e) {
// 排除数据错误的情况 比如“-” “null”类型的
formatedData += data;
}
} 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{
return "格式不正确";
}
}else{
// 最后一个值
formatedData += new BigDecimal(data).setScale(2, BigDecimal.ROUND_HALF_UP);
formatedData += ",";
data = "";
}
}
}
}
return formatedData;
}