Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> JavaScript >> Snippet

Add commas to a number


< script >
function AddComma(number) {
number = '' + number;
if (number.length > 3) {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : '' );
    for (i = 0 ; i < Math .floor(number.length / 3); i ++ ) {
        if ((mod == 0) && (i == 0))
           output += number.substring(mod + 3 * i, mod + 3 * i + 3);
        else
           output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
        }
    return (output);
    }
    else return number;
}
</ script >