//每隔三位数字加一个逗号 function moneyformat(s) { var reg = /.*\..*/; if (reg.test(s) == true) { n = 2;//设置小数点后面几位数 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; } else { return s; } } //把某些input的金额添加逗号 function ConversionMoneyformat(object) { var conversion = $("input[" + object + "]"); for (var i = 0; i < conversion.length; i++) { var st=$("input["+object+"]:eq("+i+")").val(); if(st.length>0){ $("input["+ object +"]:eq("+i+")").val(moneyformat($("input["+object+"]:eq("+i+")").val())); } } } //把某些input的金额逗号去掉 function ConversionReplace(object) { var conversion = $("input[" + object + "]"); for (var i = 0; i < conversion.length; i++) { if ($("input[" + object + "]:eq(" + i + ")").val().length > 0) { $("input[" + object + "]:eq(" + i + ")").val(($("input[" + object + "]:eq(" + i + ")").val()).replace(/,/g, "")); } } } |
转载于:https://www.cnblogs.com/chiyi/p/5664274.html