Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将我的 JavaScript 数字转换为货币数字,但可以使用任何货币符号
假设这是我的号码:
var number = 43434;
结果应该是这样的:
43,434
而不是这个:
$43,434
使用一个正则表达式/(\d)(?=(\d{3})+(?!\d))/g:
/(\d)(?=(\d{3})+(?!\d))/g
"1234255364".replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); "1,234,255,364"
要使用整数实现此目的,您可以使用+""技巧:
+""
var number = 43434; (number + "").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // 43,434