首页 > 代码库 > JS自动格式化输入的数字/千位分隔符

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

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

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