本文主要是介绍钱,金额,money的格式化输出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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的格式化输出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!