private static String getMoneyString(String money) throws Exception{
if(!isNumber(money)){
throw new Exception("格式错误:"+money);
}
NumberFormat nf = new DecimalFormat("##.00");
BigDecimal big=new BigDecimal(money);
big.setScale(2, BigDecimal.ROUND_HALF_UP);
money=nf.format(big);
if(money.startsWith(".")){
money="0"+money;
}
return money;
}
private static boolean isNumber(String str) {
Pattern pattern = Pattern.compile("[-+]?[0-9]+.?[0-9]+");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
private static String getMoneyString(BigDecimal big) throws Exception{
if(big==null){
big=new BigDecimal("0.00");
}
big.setScale(2, BigDecimal.ROUND_HALF_UP);
String money=big.toString();
NumberFormat nf = new DecimalFormat("##.00");
money=nf.format(big);
if(money.startsWith(".")){
money="0"+money;
}
return money;
}
public static void main(String[] args) throws Exception{
String m="0.08";
BigDecimal big=new BigDecimal("2.33");
System.out.println(getMoneyString(m));
}
钱,金额,money的格式化输出
最新推荐文章于 2023-12-02 19:58:53 发布