我正在尝试在 JavaScript 中打印一个整数,逗号作为千位分隔符。例如,我想将数字1234567显示为“1,234,567”。我将如何做到这一点?
这是我的做法:
function numberWithCommas(x) {
x = x.toString();
var pattern = /(-?d+)(d{3})/;
while (pattern.test(x))
x = x.replace(pattern, "$1,$2");
return x;
}
console.log(numberWithCommas(1000))
有没有更简单或更优雅的方法?如果它也适用于浮点数会很好,但这不是必需的。它不需要特定于区域设置来决定句点和逗号。
网友回答:
我很惊讶没有人提到Number.prototype.toLocaleString。
它是在JavaScript 1.5(1999年推出)中实现的,因此所有主流浏览器基本上都支持它。
var n = 34523453.345;
console.log(n.toLocaleString()); // "34,523,453.345"
它也可以在 Node 中工作.js从 v0.12 开始,通过包含 Intl
如果你想要一些不同的东西,Numeral.js可能会很有趣。
网友回答:
我使用了克里回答中的想法,但简化了它,因为我只是为我的特定目的寻找简单的东西。这是我所拥有的:
function numberWithCommas(x) {
return x.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
}
function numberWithCommas(x) {
return x.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ",");
}
function test(x, expect) {
const result = numberWithCommas(x);
const pass = result === expect;
console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);
return pass;
}
let failures = 0;
failures += !test(0, "0");
failures += !test(100, "100");
failures += !test(1000, "1,000");
failures += !test(10000, "10,000");
failures += !test(100000, "100,000");
failures += !test(1000000, "1,000,000");
failures += !test(10000000, "10,000,000");
if (failures) {
console.log(`${failures} test(s) failed`);
} else {
console.log("All tests passed");
}
.as-console-wrapper {
max-height: 100% !important;
}
正则表达式使用 2 个前瞻断言:
例如,如果你传递它,正断言将匹配 7 左侧的每个点(因为是 3 位数字的倍数,是 3 位数字的倍数,等等)。否定断言检查 3 位数字的倍数后面没有任何数字。 后面有一个句点,所以它正好是 3 位数字的倍数,所以逗号在那里。 是 3 位数字的倍数,但它后面有一个,所以这 3 位数字是一组 4 的一部分,逗号不会去那里。同样,对于 . 是 6 位数字,是 3 的倍数,因此在此之前有一个逗号。 是 3 的倍数,但它后面有一个,所以那里没有逗号。等等。防止正则表达式在字符串的开头放置逗号。123456789.01
789
678
567
789
678
9
567
456789
345678
9
B
@neu-rah提到,如果小数点后有超过3位数字,则此功能会在不需要的地方添加逗号。如果这是一个问题,您可以使用以下函数:
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ",");
return parts.join(".");
}
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/B(?=(d{3})+(?!d))/g, ",");
return parts.join(".");
}
function test(x, expect) {
const result = numberWithCommas(x);
const pass = result === expect;
console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);
return pass;
}
let failures = 0;
failures += !test(0 , "0");
failures += !test(0.123456 , "0.123456");
failures += !test(100 , "100");
failures += !test(100.123456 , "100.123456");
failures += !test(1000 , "1,000");
failures += !test(1000.123456 , "1,000.123456");
failures += !test(10000 , "10,000");
failures += !test(10000.123456 , "10,000.123456");
failures += !test(100000 , "100,000");
failures += !test(100000.123456 , "100,000.123456");
failures += !test(1000000 , "1,000,000");
failures += !test(1000000.123456 , "1,000,000.123456");
failures += !test(10000000 , "10,000,000");
failures += !test(10000000.123456, "10,000,000.123456");
if (failures) {
console.log(`${failures} test(s) failed`);
} else {
console.log("All tests passed");
}
.as-console-wrapper {
max-height: 100% !important;
}
@t.j.crowder指出,现在JavaScript有了lookback(支持信息),可以在正则表达式本身中解决:
function numberWithCommas(x) {
return x.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ",");
}
function numberWithCommas(x) {
return x.toString().replace(/B(?<!.d*)(?=(d{3})+(?!d))/g, ",");
}
function test(x, expect) {
const result = numberWithCommas(x);
const pass = result === expect;
console.log(`${pass ? "✓" : "ERROR ====>"} ${x} => ${result}`);
return pass;
}
let failures = 0;
failures += !test(0, "0");
failures += !test(0.123456, "0.123456");
failures += !test(100, "100");
failures += !test(100.123456, "100.123456");
failures += !test(1000, "1,000");
failures += !test(1000.123456, "1,000.123456");
failures += !test(10000, "10,000");
failures += !test(10000.123456, "10,000.123456");
failures += !test(100000, "100,000");
failures += !test(100000.123456, "100,000.123456");
failures += !test(1000000, "1,000,000");
failures += !test(1000000.123456, "1,000,000.123456");
failures += !test(10000000, "10,000,000");
failures += !test(10000000.123456, "10,000,000.123456");
if (failures) {
console.log(`${failures} test(s) failed`);
} else {
console.log("All tests passed");
}
.as-console-wrapper {
max-height: 100% !important;
}
(?<!.d*)
是一个负面的回溯,表示匹配项前面不能后跟零位或多位数字。负面回溯比 and 解决方案(比较)更快,至少在 V8 中是这样。.
split
join
网友回答:
下面是两个不同的浏览器 API,可以将数字转换为结构化字符串。请记住,并非所有用户的计算机都有在数字中使用逗号的区域设置。若要强制对输出使用逗号,可以使用任何“西方”区域设置,例如en-US
let number = 1234567890; // Example number to be converted
⚠️ 请注意,javascript 的最大整数值为9007199254740991
// default behaviour on a machine with a local that uses commas for numbers
let number = 1234567890;
number.toLocaleString(); // "1,234,567,890"
// With custom settings, forcing a "US" locale to guarantee commas in output
let number2 = 1234.56789; // floating point example
number2.toLocaleString('en-US', {maximumFractionDigits:2}); // "1,234.57"
//You can also force a minimum of 2 trailing digits
let number3 = 1.5;
number3.toLocaleString('en-US', {minimumFractionDigits:2, maximumFractionDigits:2}); //"1.50"
let number = 1234567890;
let nf = new Intl.NumberFormat('en-US');
nf.format(number); // "1,234,567,890"
从我检查的内容(至少是Firefox)来看,它们在性能方面或多或少是相同的。
模板简介:该模板名称为【如何将逗号的数字格式化为千位分隔符?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【JavaScript】栏目查找您需要的精美模板。