xnzt/xnzt-h5/plugins/custom/numberformat.js
2024-11-11 10:27:27 +08:00

14 lines
499 B
JavaScript

export function formatMoney(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];
var t = "";
for (var i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
return t.split("").reverse().join("") + "." + r;
}
export function replyMoney(s) {
return parseFloat(s.toString().replace(/[^\d\.-]/g, ""));
}