将逗号添加到 JavaScript 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6392102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:29:23  来源:igfitidea点击:

Add Commas to JavaScript output

javascript

提问by brianrhea

I'm using the following script to count upward at an interval and it works perfectly. However, I'd like it to format the number with commas (56,181,995 instead of 56181995).

我正在使用以下脚本以一定的间隔向上计数,并且效果很好。但是,我希望它用逗号格式化数字(56,181,995 而不是 56181995)。

var START_DATE = new Date("July 27, 2010 13:30:00"); // put in the starting date here
var INTERVAL = 1; // in seconds
var INCREMENT = 2; // increase per tick
var START_VALUE = 101; // initial value when it's the start date
var count = 0;

window.onload = function()
{
var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
document.getElementById('counter').innerHTML = count;
setInterval("count += INCREMENT; document.getElementById('counter').innerHTML = count;", msInterval);
}

I thought I had found an answer here on SO but I can't get it to work:

我以为我在 SO 上找到了答案,但我无法让它发挥作用:

How to print a number with commas as thousands separators in JavaScript

如何在 JavaScript 中使用逗号作为千位分隔符打印数字

回答by James

(1234567890).toLocaleString();

回答by Kevin Bowersox

function addCommas(nStr)
{
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '' + ',' + '');
    }
    return x1 + x2;
}

http://www.mredkj.com/javascript/nfbasic.html

http://www.mredkj.com/javascript/nfbasic.html

To integrate:

集成:

var msInterval = INTERVAL * 1000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
document.getElementById('counter').innerHTML = addCommas(count);
setInterval("count += INCREMENT; document.getElementById('counter').innerHTML = addCommas(count);", msInterval);

回答by MikeM

formatting - How can I format numbers as money in JavaScript?

格式化 - 如何在 JavaScript 中将数字格式化为货币?

using this SO post on formatting money as a basis, you can re-engineer this to work for just comma separation

使用这篇关于格式化货币的 SO 帖子作为基础,您可以重新设计它以仅用于逗号分隔

here's an example - http://jsfiddle.net/pxfunc/etfjW/

这是一个例子 - http://jsfiddle.net/pxfunc/etfjW/

You can extend the javascript Numbertype to include a commaSeparatedformatter method like so:

您可以扩展 javascriptNumber类型以包含一个commaSeparated格式化程序方法,如下所示:

Number.prototype.commaSeparated = function() {
    var n = this,
        t = ",",
        s = n < 0 ? "-" : "",
        i = parseInt(n = Math.abs(+n || 0)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "" + t);
};

then call it like so

然后这样称呼它

count.commaSeparated();

or

或者

(1234567890).commaSeparated();

回答by vol7ron

function addCommas(str){
   var arr,int,dec;
   str += '';

   arr = str.split('.');
   int = arr[0] + '';
   dec = arr.length>1?'.'+arr[1]:'';

   return int.replace(/(\d)(?=(\d{3})+$)/g,",") + dec;
}