public class CommonUtils {
public static String handlePrice(String price){
DecimalFormat df =new DecimalFormat("#####0.00");
if (isDoubleOrFloat(price)){
String str = df.format(Double.parseDouble(price));
return str;
}else {
return "请输入正确格式的价格";
}
}
/*
* 是否为浮点数?double或float类型。
* @param str 传入的字符串。
* @return 是浮点数返回true,否则返回false。
*/
public static boolean isDoubleOrFloat(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
return pattern.matcher(str).matches();
}
}