The most effective way to format numbers on Arduino
Here is my try at it: The usual way to write a number in decimal is to
get the digits from right to left with a loop that looks something like



digit = '0' + val % 10;
val /= 10;


The following function does exactly this, with the addition of adding
commas at the correct positions:

/*
 * Format an unsigned long (32 bits) into a string in the format
 * "23,854,972".
 *
 * The provided buffer must be at least 14 bytes long. The number will
 * be right-adjusted in the buffer. Returns a pointer to the first
 * digit.
 */
char *ultoa(unsigned long val, char *s)
{
    char *p = s + 13;
    *p = '\0';
    do {
        if ((p - s) % 4 == 2)
            *--p = ',';
        *--p = '0' + val % 10;
        val /= 10;
    } while (val);
    return p;
}


Note that the string is right adjusted in the buffer, and the unused
characters are not touched. This means you can get a right adjusted
string by:


pre-filling the string with spaces (for padding)
calling the function above
using the original buffer as the right adjusted string


Otherwise you would use the returned pointer to have a string with no
padding.

Here is a simple test program:

unsigned long vals[] = {0, 1, 2, 9, 10, 999, 1000, 0xffffffff};
const int COUNT = sizeof vals / sizeof vals[0];

void setup()
{
    Serial.begin(9600);
    char buffer[14];
    for (int i = 0; i &lt; COUNT; i++) {
        unsigned long val = vals[i];
        Serial.print(val);
        Serial.write('\t');
        Serial.println(ultoa(val, buffer));
    }
}

void loop() {}