如何在JavaScript中使用逗号作为千位分隔符来打印数字 [英] How to print a number with commas as thousands separators in JavaScript

查看:219
本文介绍了如何在JavaScript中使用逗号作为千位分隔符来打印数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 JavaScript 中使用逗号作为千位分隔符来打印整数。例如,我想显示编号1234567为1,234,567。我怎么去做这个?



这是我如何做的:

pre $函数numberWithCommas( x){
x = x.toString();
var pattern = /( - ?\d +)(\d {3})/;
while(pattern.test(x))
x = x.replace(pattern,$ 1,$ 2);
return x;





$ b

有没有更简单或更优雅的方式来做到这一点?这将是很好,如果它也适用于浮动,但这不是必要的。它不需要特定于语言环境来决定句点和逗号。

解决方案

我使用了Kerry的答案,但简化了它,因为我只是为了特定目的寻找简单的东西。这是我做的:

  const numberWithCommas =(x)=> {
return x.toString()。replace(/ \B(?=(\d {3})+(?! \d))/ g,,);



$ b $ p
$ b

这就是你真正需要知道的。



@Neils Bom询问正则表达式是如何工作的。我的解释有点长。它不适合的评论,我不知道还有什么地方,所以我在这里做。如果有人有任何其他的建议,请告诉我。



正则表达式使用2个前瞻断言:正面的一个查找字符串中的任意点在它之后的一行中有3位数的倍数,并且有一个否定的断言来确保这个点只有3位数的整数倍。例如,如果你通过它123456789.01,肯定的断言将匹配7的左边的每一个点(因为/ 789是3位的倍数,678是3位的倍数,567等)。否定断言检查3位数的倍数之后没有任何数字。 789之后有一段时间,所以它恰好是3位数的倍数,所以逗号在那里。 678是3位数的倍数,但它后面有一个9,所以这3位数字是一组4的一部分,逗号不会去那里。类似的567。 456789是6位数字,是3的倍数,所以前面有一个逗号。 345678是3的倍数,但它之后有一个9,所以没有逗号。等等。 \B使得正则表达式不会在字符串的开头加一个逗号。



@ neu-rah提到这个函数在不需要的地方添加了逗号小数点后3位以上。如果这是一个问题,你可以使用这个函数:

  const numberWithCommas =(x)=> {
var parts = x.toString()。split(。);
parts [0] = parts [0] .replace(/ \B(?=(\d {3})+(?! \ d))/ g,,);
return parts.join(。);
}


I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this?

Here is how I am doing it:

function numberWithCommas(x) {
    x = x.toString();
    var pattern = /(-?\d+)(\d{3})/;
    while (pattern.test(x))
        x = x.replace(pattern, "$1,$2");
    return x;
}

Is there a simpler or more elegant way to do it? It would be nice if it works with floats also, but that is not necessary. It does not need to be locale-specific to decide between periods and commas.

解决方案

I used the idea from Kerry's answer, but simplified it since I was just looking for something simple for my specific purpose. Here is what I did:

const numberWithCommas = (x) => {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

This is all you really need to know.

@Neils Bom asked how the regex works. My explanation is sort of long. It won't fit in the comments and I don't know where else to put it so I am doing it here. If anyone has any other suggestions for where to put it, please let me know.

The regex uses 2 lookahead assertions: a positive one to look for any point in the string that has a multiple of 3 digits in a row after it, and a negative assertion to make sure that point only has exactly a multiple of 3 digits. The replacement expression puts a comma there.

For example, if you pass it "123456789.01", the positive assertion will match every spot to the left of the 7 (since "789" is a multiple of 3 digits, "678" is a multiple of 3 digits, "567", etc.). The negative assertion checks that the multiple of 3 digits does not have any digits after it. "789" has a period after it so it is exactly a multiple of 3 digits, so a comma goes there. "678" is a multiple of 3 digits but it has a "9" after it, so those 3 digits are part of a group of 4, and a comma does not go there. Similarly for "567". "456789" is 6 digits, which is a multiple of 3, so a comma goes before that. "345678" is a multiple of 3, but it has a "9" after it, so no comma goes there. And so on. The "\B" keeps the regex from putting a comma at the beginning of the string.

@neu-rah mentioned that this function adds commas in undesirable places if there are more than 3 digits after the decimal point. If this is a problem, you can use this function:

const numberWithCommas = (x) => {
  var parts = x.toString().split(".");
  parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  return parts.join(".");
}

这篇关于如何在JavaScript中使用逗号作为千位分隔符来打印数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆