每三位数字加逗号 [英] Add comma to numbers every three digits

查看:81
本文介绍了每三位数字加逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery使用每三个数字使用逗号分隔符格式化数字?

How can I format numbers using a comma separator every three digits using jQuery?

例如:

╔═══════════╦═════════════╗
║   Input   ║   Output    ║
╠═══════════╬═════════════╣
║       298 ║         298 ║
║      2984 ║       2,984 ║
║ 297312984 ║ 297,312,984 ║
╚═══════════╩═════════════╝

推荐答案

@Paul Creasey具有最简单的正则表达式解决方案,但它是一个简单的jQuery插件:

@Paul Creasey had the simplest solution as the regex, but here it is as a simple jQuery plugin:

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
}

然后您可以像这样使用它:

You could then use it like this:

$("span.numbers").digits();

这篇关于每三位数字加逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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