js实现给数字加三位一逗号间隔的两种方法
方法一:
- <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>