<script language="javascript"> function formatNum(num,n) {//参数说明:num 要格式化的数字 n 保留小数位 num = String(num.toFixed(n)); var re = /(-?\d+)(\d{3})/; while(re.test(num)) num = num.replace(re,"$1,$2") return num; } alert(formatNum(1234005651.789,2)); </script> //Javascript 格式化金额 //格式化: function fmoney(s, n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + ""; var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1]; t = ""; for(i = 0; i < l.length; i ++ ) { t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); } return t.split("").reverse().join("") + "." + r; } 复原: function rmoney(s) { return parseFloat(s.replace(/[^\d\.-]/g, "")); }
java格式化金额,数字 import java.text.DecimalFormat; import java.text.NumberFormat; public class TestFormatter { public static void main(String[] args) { String str = "0600450625465.5626"; TestFormatter t = new TestFormatter(); System.out.println(t.getFormatter(str)); System.out.println(t.getCurrency(str)); System.out.println(t.getDecimalFormat(str)); } private String getFormatter(String str) { NumberFormat n = NumberFormat.getNumberInstance(); double d; String outStr = null; try { d = Double.parseDouble(str); outStr = n.format(d); } catch (Exception e) { e.printStackTrace(); } return outStr; } private String getDecimalFormat(String str){ DecimalFormat fmt = new DecimalFormat("###,###.##"); //也可用000,000.00代替 String outStr = null; double d; try { d = Double.parseDouble(str); outStr = fmt.format(d); } catch (Exception e) { } return outStr; } private String getCurrency(String str) { NumberFormat n = NumberFormat.getCurrencyInstance(); double d; String outStr = null; try { d = Double.parseDouble(str); outStr = n.format(d); } catch (Exception e) { e.printStackTrace(); } return outStr; } } 测试结果 600,450,625,465.563 ¥600,450,625,466 600,450,625,465.56260
javascript格式化金额为xxxxxxx.00,即保留两位小数。
parseFloat(1000).toFixed(2)