jQuery:将数字字符串转换为某种标准格式

时间:2012-07-11 09:28:12

标签: jquery

是否有任何jQuery插件可以将数字字符串转换为标准格式,例如:

200000 to 2,00,000
1000 to 1,000
398740 to 3,987,40

依旧......

1 个答案:

答案 0 :(得分:1)

试试这个:

var s = 200000;
var converted = s.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")

DEMO

或:

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

你可以调用它来转换所选元素的文本,就像其他jQuery方法一样:

$('#elem').convert()