숫자에 세자리마다 콤마를 찍는 알고리즘

|
int
util_add_comma_to_num(const char *str, char *buf, int buflen)
{
    int len;
    int shift;

    /* count given string */
    len = strlen(str);
    shift = -len;

    assert( buflen >= (len + len/3 +1));

    while (*str)
    {
        *buf++ = *str++;
        if (++shift && (shift % 3) == 0)
            *buf++= ',';
    }

    *buf = '\0';

    return 0;
}

출처 : http://kldp.org/node/38269

Trackback 0 And Comment 0