JS 实现给数字加三位一逗号间隔的两种方法
方法一:
<script type= "text/javascript"> var num_s = "1232134456.546"; console.log(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"; console.log(format_number(a)); console.log(format_number("wahh")); console.log(format_number(0)); console.log(format_number(6698.0023)); </script>