每隔三位数字为数字添加逗号如何使用jQuery使用逗号分隔符每三位数格式化数字?例如:╔═══════════╦═════════════╗║ Input ║ Output ║╠═══════════╬═════════════╣║ 298 ║ 298 ║║ 2984 ║ 2,984 ║║ 297312984 ║ 297,312,984 ║╚═══════════╩═════════════╝
3 回答

绝地无双
TA贡献1946条经验 获得超4个赞
有最简单的解决方案作为正则表达式,但这里它是一个简单的jQuery插件:
$.fn.digits = function(){ return this.each(function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); })}
然后你可以像这样使用它:
$("span.numbers").digits();

明月笑刀无情
TA贡献1828条经验 获得超4个赞
你可以使用Number.toLocaleString()
:
var number = 1557564534;document.body.innerHTML = number.toLocaleString();// 1,557,564,534

海绵宝宝撒
TA贡献1809条经验 获得超8个赞
如果你正在使用正则表达式,那就是这样的东西,不确定替换的确切语法!
MyNumberAsString.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
- 3 回答
- 0 关注
- 696 浏览
添加回答
举报
0/150
提交
取消