Monday, 29 September 2014

Getting commas in Javascript

function numberWithCommaslengthmsg(Value) {
    var x = Value;
    var numbers = /^[0-9\,]+$/;
    var temp_val = Value;
    if (numbers.test(temp_val)) {
        x = x.toString().replace(/,/gi, '');
        var lastThree = x.substring(x.length - 3);
        var otherNumbers = x.substring(0, x.length - 3);
        if (otherNumbers != '')
            lastThree = ',' + lastThree;
        var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
        return res;
    }
    return 0;
}

Input:150000
Output:: 1,50,000

No comments:

Post a Comment