2

How can I modify the function below to make it stop adding commas once we hit the decimal marker?

addCommas = function(number) {
    if(number === undefined) {
        return '';
    }

    while(/(\d+)(\d{3})/.test(number.toString())) {
        number = number.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }

    return number;
}

addCommas(0.123456); // Outputs 0.123,456, should output 0.123456
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
  • set a boolean flag to `true` when `.` was encountered. – hjpotter92 Nov 05 '14 at 18:57
  • 1
    If this isn't just an exercise for you to learn something, I'd probably use numeral.js: http://numeraljs.com/ – Paul Nov 05 '14 at 19:00
  • possible duplicate of [How to print a number with commas as thousands separators in JavaScript](http://stackoverflow.com/q/2901102/1048572) – Bergi Nov 05 '14 at 19:01

4 Answers4

5

My method is to split the number into fractional and integer parts...

addCommas = function(number) 
{
    if (number === undefined)
        return '';
    var parts = number.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}

document.write( addCommas(0.123456) + "<br />" );
document.write( addCommas(123456.123456) + "<br />" );
document.write( addCommas(123456) + "<br />" );
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
4

Just add ^[^.]* to the front of the pattern:

addCommas = function(number) {
    if(number === undefined) {
        return '';
    }

    while(/^[^.]*(\d+)(\d{3})/.test(number.toString())) {
        number = number.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }

    return number;
}

You only have to do that for the .test() call.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

I would break the string in two prior to using the regular expression. Such as:

RenanCommas = function(number) {
    if(number === undefined) {
        return '';
    }

    var parts = (number + "").split(".");
    var integerPart = parts[0];
    var decimalPart = parts.length > 1 ? parts[1] : "0";

    while(/(\d+)(\d{3})/.test(integerPart.toString())) {
        integerPart = integerPart.replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }

    return integerPart + "." + decimalPart;
}

Notice that you could also have decimalPart as an empty string. In that case, you could check whether or not it is empty before using it. If you use an empty string instead of "0", the last line would be something like:

    return integerPart + (decimalPart ? ("." + decimalPart) : "");

And when you run it:

RenanCommas(12121241243.1123131232); // outputs "12,121,241,243.112312"
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
1

This seems to be working:

addCommas = function(number) {
   return number.replace(/(\.\d+$)|(?!^)(?=(?:\d{3})+(?=\.|$))/g, function($0, $1) {
       return ($1)?$1:','; });
}

Examples:

addCommas('0.123456');
"0.123456"
addCommas('987654.123456');
"987,654.123456"
addCommas('987654321');
"987,654,321"
anubhava
  • 761,203
  • 64
  • 569
  • 643