JS实现转换千分位计数
350000.00-------350,000.00
var num=0; function format (num) { return (num.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,'); }
var num = 12345; console.log(format(num));-----------------输出12,345.00
js方法toFixed(n):将number转换为小数点后剩余n位 ; 例如 element.toFixed(2) -----element.00
replace替代,
'$&,'按条件替代
<body> <input type="text" id="txtNumber" /><input type="button" id="btnConvert" value="换算" /><br /> <span id="spaResult"></span> <script type="text/javascript"> <!-- document.all.btnConvert.onclick = function() { var strInput = document.all.txtNumber.value; if (strInput != "") { var numInput = parseFloat(strInput); document.all.spaResult.innerText = numInput/10000 + "万"; } }; // 1000010 --> 100.001万 // 2034 --> 0.2034万 // 30000 --> 3万 //--> </script> </body>