JS自动格式化输入的数字/千位分隔符VIEW:858

时间:2021-01-02 23:39:58

JS自动格式化输入的数字/千位分隔符VIEW:858

<script>
function cc(s){
if(/[^0-9\.]/.test(s)) return "invalid value";
ss=s.replace(/^(\d*)$/,"$1.");
s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
ss=s.replace(".",",");
var re=/(\d)(\d{3},)/;
while(re.test(s))
ss=s.replace(re,"$1,$2");
ss=s.replace(/,(\d\d)$/,".$1");
return "¥" + s.replace(/^\./,"0.")
}
</script>
<input onchange="this.value=cc(this.value)">