如何在 JavaScript 中用逗号打印一个数字作为千位分隔符
其他问答
1
我需要用逗号作为千位分隔符在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; } numberWithCommas(123123123)
-
function numberWithCommas(x) { x = x.toString(); var pattern = /(-?\d+)(\d{3})/; while (pattern.test(x)) x = x.replace(pattern, "$1,$2"); return x; }
发表回复