You must Sign In to post a response.
  • Category: JavaScript

    Comma Separated value in javascript

    Hi to all

    i have a number 123456. i get this value from database and display that.. when i display the value like that 1,23,456... How can i do this using Javascript code.......



    Thanks in Regards
    ----------------------
    Saravana Perumal.N
  • #664995
    Try this code ....

    function addCommas(nStr)
    {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
    }

    Examples
    addCommas(1000)
    // 1,000

    addCommas(1231.897243)
    // 1,231.897243

    addCommas('9999999.00')
    // 9,999,999.00

    addCommas(-500000.99)
    // -500,000.99


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.