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 "請輸入正確格式的價格";
}
}
/*
* 是否為浮點數(shù)嘱丢?double或float類型漠趁。
* @param str 傳入的字符串。
* @return 是浮點數(shù)返回true,否則返回false。
*/
? ? public static boolean isDoubleOrFloat(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
return pattern.matcher(str).matches();
}
}