vForums Support > Programming & Coding :: Database of Codes/Hacks/Mods :: Code Submissions :: > Add Comma

Add Comma - Posted By Michael (wrighty) on 10th Jun 08 at 10:13am
This code will change long numbers into numbers will commas in.. eg: 12121212 = 12,121,212 {Smile}

<script>
/*Add Commas - Snippet*/

function comma_it(n){
    n += '';
    x = n.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;
}
</script>


Example:

<script>
alert(comma_it('121211221'))
</script>


{Smile}