android中处理价格的问题,价格保留小数点后两位

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();

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容