function amount(s,n) {
// s amount n 保留的小数位数
if(n===undefined)
n=2;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1], t = "";
for (var i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
var res=t.split("").reverse().join("");
if(n==0)
return res;
return res + "." + r;
}
amount(321321,2) // 321,321.20
网友评论