JS 数字转化为货币格式

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

 

posted on 2015-12-17 21:23  理智与情感  阅读(159)  评论(0编辑  收藏  举报