1

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

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

等等..

4

1 回答 1

1

试试这个:

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

演示

或者:

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

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

$('#elem').convert()
于 2012-07-11T09:31:29.527 回答