PHP Ferrari
PHP Ferrari

Reputation: 15616

jQuery: Convert numeric string in to some standard format

Is there any jQuery plugin which convert numeric string into a standard format let say:

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

and so on..

Upvotes: 1

Views: 732

Answers (1)

Ram
Ram

Reputation: 144659

try this:

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

DEMO

or:

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

you can call it for converting texts of selected elements like other jQuery methods:

$('#elem').convert()

Upvotes: 1

Related Questions