js实现金额千分位格式化的处理函数
2017-05-02 本文已影响390人
村长很忙
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="jquery-1.10.1.min.js"></script>
</head>
<body>
<input type="text" value="">
<script type="text/javascript">
$('input').blur('input propertychange', function() {
var test = $(this).val();
var money = fmoney(test);
console.log(money);
$(this).val(money);
});
function fmoney(val) {
n = 2;
s= val;
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;
}
</script>
</body>
</html>