每三位数字加逗号
我如何使用逗号分隔符使用jQuery每三位数字格式化数字?
例如:
╔═══════════╦═════════════╗ ║ Input ║ Output ║ ╠═══════════╬═════════════╣ ║ 298 ║ 298 ║ ║ 2984 ║ 2,984 ║ ║ 297312984 ║ 297,312,984 ║ ╚═══════════╩═════════════╝ @Paul Creasey有最简单的解决scheme作为正则expression式,但在这里它是一个简单的jQuery插件:
 $.fn.digits = function(){ return this.each(function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); }) } 
你可以像这样使用它:
 $("span.numbers").digits(); 
像这样的东西,如果你正在进入正则expression式,不确定replace寿命的确切语法!
 MyNumberAsString.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); 
 你可以使用Number.toLocaleString() : 
 var number = 1557564534; document.body.innerHTML = number.toLocaleString(); // 1,557,564,534 
你可以尝试NumberFormatter 。
 $(this).format({format:"#,###.00", locale:"us"}); 
它也支持不同的语言环境,当然包括美国。
下面是一个非常简单的如何使用它的例子:
 <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.numberformatter.js"></script> <script> $(document).ready(function() { $(".numbers").each(function() { $(this).format({format:"#,###", locale:"us"}); }); }); </script> </head> <body> <div class="numbers">1000</div> <div class="numbers">2000000</div> </body> </html> 
输出:
 1,000 2,000,000 
这不是jQuery,但它适用于我。 采取从这个站点 。
 function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } 
更彻底的解决scheme
 这个的核心是replace呼叫。 到目前为止,我不认为任何build议的解决scheme将处理以下所有情况: 
-  整数: 1000 => '1,000'
-  string: '1000' => '1,000'
-  对于string:
-  保留小数点后的零: 10000.00 => '10,000.00'
-  在十进制前丢弃前导零: '01000.00 => '1,000.00'
-  小数点后不加逗号: '1000.00000' => '1,000.00000'
-  保留领先-或+:'-1000.0000' => '-1,000.000'
-  返回未修改的包含非数字的string: '1000k' => '1000k'
 
-  保留小数点后的零: 
以下function完成以上所有function。
 addCommas = function(input){ // If the regex doesn't match, `replace` returns the string unmodified return (input.toString()).replace( // Each parentheses group (or 'capture') in this regex becomes an argument // to the function; in this case, every argument after 'match' /^([-+]?)(0?)(\d+)(.?)(\d+)$/g, function(match, sign, zeros, before, decimal, after) { // Less obtrusive than adding 'reverse' method on all strings var reverseString = function(string) { return string.split('').reverse().join(''); }; // Insert commas every three characters from the right var insertCommas = function(string) { // Reverse, because it's easier to do things from the left var reversed = reverseString(string); // Add commas every three characters var reversedWithCommas = reversed.match(/.{1,3}/g).join(','); // Reverse again (back to normal) return reverseString(reversedWithCommas); }; // If there was no decimal, the last capture grabs the final digit, so // we have to put it back together with the 'before' substring return sign + (decimal ? insertCommas(before) + decimal + after : insertCommas(before + after)); } ); }; 
你可以像这样在一个jQuery插件中使用它:
 $.fn.addCommas = function() { $(this).each(function(){ $(this).text(addCommas($(this).text())); }); }; 
使用函数Number();
 $(function() { var price1 = 1000; var price2 = 500000; var price3 = 15245000; $("span#s1").html(Number(price1).toLocaleString('en')); $("span#s2").html(Number(price2).toLocaleString('en')); $("span#s3").html(Number(price3).toLocaleString('en')); console.log(Number(price).toLocaleString('en')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <span id="s1"></span><br /> <span id="s2"></span><br /> <span id="s3"></span><br /> 
2016答:
Javascript有这个function,所以不需要Jquery。
 yournumber.toLocaleString("en"); 
你也可以看看jQuery FormatCurrency插件(我是作者); 它也支持多种语言环境,但是可能会有您不需要的货币支持的开销。
 $(this).formatCurrency({ symbol: '', roundToDecimalPlace: 0 });