js 现给数字加三位一逗号间隔的种方法

方法一:(ie下有问题)

     <script  type= "text/javascript">

         var   num_s = "1232134456.546 ";alert(parseFloat(num_s).toLocaleString());

     </script>

方法二: 

 <script type="text/javascript">
   function format_number(n){
   var b=parseInt(n).toString();
   var len=b.length;
   if(len<=3){return b;}
   var r=len%3;
   return r>0?b.slice(0,r)+","+b.slice(r,len).match(/\d{3}/g).join(","):b.slice(r,len).match(/\d{3}/g).join(",");
 }

var a="53669988.000";
alert(format_number(a));
alert(format_number("wahh"));
alert(format_number(0));
alert(format_number(6698.0023));
</script>

  

posted @ 2015-05-04 14:16  淡然_  阅读(4581)  评论(0编辑  收藏  举报