programing

C에서 1123456789에서 1,123,456,789로 숫자를 포맷하는 방법은 무엇입니까?

nasanasas 2020. 10. 29. 08:13
반응형

C에서 1123456789에서 1,123,456,789로 숫자를 포맷하는 방법은 무엇입니까?


C 언어 형식으로 숫자를에서 1123456789까지 어떻게 할 수 1,123,456,789있습니까? 나는 사용해 printf("%'10d\n", 1123456789);보았지만 작동하지 않습니다.

조언 해 주시겠습니까? 솔루션이 간단할수록 좋습니다.


printf가 '플래그를 지원하는 경우 (POSIX 2008에서 요구하는대로 printf()), 로케일을 적절하게 설정하여 수행 할 수 있습니다. 예:

#include <stdio.h>
#include <locale.h>

int main(void)
{
    setlocale(LC_NUMERIC, "");
    printf("%'d\n", 1123456789);
    return 0;
}

그리고 빌드 및 실행 :

$ ./example 
1,123,456,789

Mac OS X 및 Linux (Ubuntu 10.10)에서 테스트되었습니다.


다음과 같이 재귀 적으로 수행 할 수 있습니다 ( INT_MIN2의 보수를 사용하는 경우이를 관리하려면 추가 코드가 필요합니다).

void printfcomma2 (int n) {
    if (n < 1000) {
        printf ("%d", n);
        return;
    }
    printfcomma2 (n/1000);
    printf (",%03d", n%1000);
}

void printfcomma (int n) {
    if (n < 0) {
        printf ("-");
        n = -n;
    }
    printfcomma2 (n);
}

요약 :

  • 사용자 printfcomma는 정수로 호출합니다. 음수의 특수한 경우는 단순히 "-"를 인쇄하고 숫자를 양수로 만들어 처리합니다 (이는에서 작동하지 않는 비트입니다 INT_MIN).
  • 를 입력하면 printfcomma21,000 미만의 숫자 만 인쇄되고 반환됩니다.
  • 그렇지 않으면 1,000 미만의 숫자를 찾을 때까지 다음 레벨에서 재귀가 호출됩니다 (따라서 1,234,567은 1,234로 호출 된 다음 1).
  • 그런 다음 그 숫자가 인쇄되고 재귀 트리로 돌아가서 쉼표와 다음 숫자를 인쇄합니다.

모든 수준 에서 음수를 검사 할 때 불필요한 처리를 수행하지만 더 간결한 버전도 있습니다 (제한된 수의 재귀 수준을 고려할 때 중요하지 않음). 이것은 테스트를위한 완전한 프로그램입니다.

#include <stdio.h>

void printfcomma (int n) {
    if (n < 0) {
        printf ("-");
        printfcomma (-n);
        return;
    }
    if (n < 1000) {
        printf ("%d", n);
        return;
    }
    printfcomma (n/1000);
    printf (",%03d", n%1000);
}

int main (void) {
    int x[] = {-1234567890, -123456, -12345, -1000, -999, -1,
               0, 1, 999, 1000, 12345, 123456, 1234567890};
    int *px = x;
    while (px != &(x[sizeof(x)/sizeof(*x)])) {
        printf ("%-15d: ", *px);
        printfcomma (*px);
        printf ("\n");
        px++;
    }
    return 0;
}

출력은 다음과 같습니다.

-1234567890    : -1,234,567,890
-123456        : -123,456
-12345         : -12,345
-1000          : -1,000
-999           : -999
-1             : -1
0              : 0
1              : 1
999            : 999
1000           : 1,000
12345          : 12,345
123456         : 123,456
1234567890     : 1,234,567,890

재귀를 신뢰하지 않는 사람들을위한 반복적 인 솔루션 (재귀의 유일한 문제는 스택 공간이되는 경향이 있지만 64 비트 정수의 경우에도 몇 수준 만 깊기 때문에 여기서는 문제가되지 않습니다) :

void printfcomma (int n) {
    int n2 = 0;
    int scale = 1;
    if (n < 0) {
        printf ("-");
        n = -n;
    }
    while (n >= 1000) {
        n2 = n2 + scale * (n % 1000);
        n /= 1000;
        scale *= 1000;
    }
    printf ("%d", n);
    while (scale != 1) {
        scale /= 1000;
        n = n2 / scale;
        n2 = n2  % scale;
        printf (",%03d", n);
    }
}

이 두 생성 2,147,483,647을 위해 INT_MAX.


다음은 매우 간단한 구현입니다. 이 함수는 오류 검사를 포함 하지 않으며 버퍼 크기는 호출자가 확인해야합니다. 음수에도 작동하지 않습니다. 이러한 개선 사항은 독자를위한 연습으로 남겨집니다.

void format_commas(int n, char *out)
{
    int c;
    char buf[20];
    char *p;

    sprintf(buf, "%d", n);
    c = 2 - strlen(buf) % 3;
    for (p = buf; *p != 0; p++) {
       *out++ = *p;
       if (c == 1) {
           *out++ = ',';
       }
       c = (c + 1) % 3;
    }
    *--out = 0;
}

Egads! 저는 리눅스에서 gcc / g ++와 glibc를 사용하여 항상 이것을합니다. 그리고 그렇습니다.

#include <stdio.h>
#include <locale.h>

int main()
{
    int bignum=12345678;

    setlocale(LC_ALL,"");

    printf("Big number: %'d\n",bignum);

    return 0;
}

다음과 같은 출력을 제공합니다.

큰 숫자 : 12,345,678

거기에있는 'setlocale'호출을 기억해야합니다. 그렇지 않으면 아무것도 포맷하지 않습니다.


로케일 인식 버전이 흥미로울 것입니다.

#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <limits.h>

static int next_group(char const **grouping) {
    if ((*grouping)[1] == CHAR_MAX)
        return 0;
    if ((*grouping)[1] != '\0')
        ++*grouping;
    return **grouping;
}

size_t commafmt(char   *buf,            /* Buffer for formatted string  */
                int     bufsize,        /* Size of buffer               */
                long    N)              /* Number to convert            */
{
    int i;
    int len = 1;
    int posn = 1;
    int sign = 1;
    char *ptr = buf + bufsize - 1;

    struct lconv *fmt_info = localeconv();
    char const *tsep = fmt_info->thousands_sep;
    char const *group = fmt_info->grouping;
    char const *neg = fmt_info->negative_sign;
    size_t sep_len = strlen(tsep);
    size_t group_len = strlen(group);
    size_t neg_len = strlen(neg);
    int places = (int)*group;

    if (bufsize < 2)
    {
ABORT:
        *buf = '\0';
        return 0;
    }

    *ptr-- = '\0';
    --bufsize;
    if (N < 0L)
    {
        sign = -1;
        N = -N;
    }

    for ( ; len <= bufsize; ++len, ++posn)
    {
        *ptr-- = (char)((N % 10L) + '0');
        if (0L == (N /= 10L))
            break;
        if (places && (0 == (posn % places)))
        {
            places = next_group(&group);
            for (int i=sep_len; i>0; i--) {
                *ptr-- = tsep[i-1];
                if (++len >= bufsize)
                    goto ABORT;
            }
        }
        if (len >= bufsize)
            goto ABORT;
    }

    if (sign < 0)
    {
        if (len >= bufsize)
            goto ABORT;
        for (int i=neg_len; i>0; i--) {
            *ptr-- = neg[i-1];
            if (++len >= bufsize)
                goto ABORT;
        }
    }

    memmove(buf, ++ptr, len + 1);
    return (size_t)len;
}

#ifdef TEST
#include <stdio.h>

#define elements(x) (sizeof(x)/sizeof(x[0]))

void show(long i) {
    char buffer[32];

    commafmt(buffer, sizeof(buffer), i);
    printf("%s\n", buffer);
    commafmt(buffer, sizeof(buffer), -i);
    printf("%s\n", buffer);
}


int main() {

    long inputs[] = {1, 12, 123, 1234, 12345, 123456, 1234567, 12345678 };

    for (int i=0; i<elements(inputs); i++) {
        setlocale(LC_ALL, "");
        show(inputs[i]);
    }
    return 0;
}

#endif

이것은 버그가 있습니다 (하지만 상당히 사소하다고 생각하는 버그입니다). 2의 보수 하드웨어에서는 음수를 N = -N;2의 보수로 동등한 양수로 변환하려고 시도하기 때문에 가장 음수를 올바르게 변환 하지 않습니다. 그렇지 않으면 최대 음수에는 해당하는 양수가 없습니다. 더 큰 유형으로 승격하십시오. 이 문제를 해결하는 한 가지 방법은 해당하는 서명되지 않은 유형으로 번호를 승격하는 것입니다 (하지만 다소 사소하지 않습니다).


재귀 또는 문자열 처리없이 수학적 접근 방식 :

#include <stdio.h>
#include <math.h>

void print_number( int n )
{
    int order_of_magnitude = (n == 0) ? 1 : (int)pow( 10, ((int)floor(log10(abs(n))) / 3) * 3 ) ;

    printf( "%d", n / order_of_magnitude ) ;

    for( n = abs( n ) % order_of_magnitude, order_of_magnitude /= 1000;
        order_of_magnitude > 0;
        n %= order_of_magnitude, order_of_magnitude /= 1000 )
    {
        printf( ",%03d", abs(n / order_of_magnitude) ) ;
    }
}

원칙적으로 Pax의 재귀 솔루션과 유사하지만 사전에 크기 순서를 계산하면 재귀를 피할 수 있습니다 (아마도 상당한 비용이 듭니다).

천 단위를 구분하는 데 사용되는 실제 문자는 로케일에 따라 다릅니다.

편집 : 개선 사항은 아래 @Chux의 의견을 참조하십시오.


@Greg Hewgill을 기반으로하지만 음수를 고려하여 문자열 크기를 반환합니다.

size_t str_format_int_grouped(char dst[16], int num)
{
    char src[16];
    char *p_src = src;
    char *p_dst = dst;

    const char separator = ',';
    int num_len, commas;

    num_len = sprintf(src, "%d", num);

    if (*p_src == '-') {
        *p_dst++ = *p_src++;
        num_len--;
    }

    for (commas = 2 - num_len % 3;
         *p_src;
         commas = (commas + 1) % 3)
    {
        *p_dst++ = *p_src++;
        if (commas == 1) {
            *p_dst++ = separator;
        }
    }
    *--p_dst = '\0';

    return (size_t)(p_dst - dst);
}

또 다른 반복 함수

int p(int n) {
  if(n < 0) {
    printf("-");
    n = -n;
  }

  int a[sizeof(int) * CHAR_BIT / 3] = { 0 };
  int *pa = a;
  while(n > 0) {
    *++pa = n % 1000;
    n /= 1000;
  }
  printf("%d", *pa);
  while(pa > a + 1) {
    printf(",%03d", *--pa);
  }
}

다음은 이러한 종류의 십진수 형식을 가장 얇고, 크기 및 속도 효율적으로 구현 한 것입니다.

const char *formatNumber (
    int value,
    char *endOfbuffer,
    bool plus)
{
    int savedValue;
    int charCount;

    savedValue = value;
    if (unlikely (value < 0))
        value = - value;
    *--endOfbuffer = 0;
    charCount = -1;
    do
    {
        if (unlikely (++charCount == 3))
        {
            charCount = 0;
            *--endOfbuffer = ',';
        }

        *--endOfbuffer = (char) (value % 10 + '0');
    }
    while ((value /= 10) != 0);

    if (unlikely (savedValue < 0))
        *--endOfbuffer = '-';
    else if (unlikely (plus))
        *--endOfbuffer = '+';

    return endOfbuffer;
}

다음과 같이 사용하십시오 :

char buffer[16];
fprintf (stderr, "test : %s.", formatNumber (1234567890, buffer + 16, true));

산출:

test : +1,234,567,890.

몇 가지 장점 :

  • 역순 포맷으로 인해 문자열 버퍼의 끝을 차지하는 함수입니다. 마지막으로 생성 된 문자열 (strrev)을 고려할 필요가 없습니다.

  • 이 함수는 모든 알고리즘에서 사용할 수있는 하나의 문자열을 생성합니다. 끔찍하게 느리고 항상 컨텍스트에 따라 달라지는 여러 printf / sprintf 호출에 의존하거나 필요하지 않습니다.

  • 나누기 연산자의 최소 수 (/, %).

또 다른 솔루션은 결과를 int 배열에 저장하여 long long int로 인해 7이면 최대 크기가 9,223,372,036,854,775,807에서 -9,223,372,036,854,775,807 범위의 숫자를 처리 할 수 ​​있습니다. note it is not an unsigned

비 재귀 인쇄 기능

static void printNumber (int numbers[8], int loc, int negative)
{
    if (negative)
    {
        printf("-");
    }
    if (numbers[1]==-1)//one number
    {
        printf("%d ", numbers[0]);
    }
    else
    {
        printf("%d,", numbers[loc]);
        while(loc--)
        {
            if(loc==0)
            {// last number
                printf("%03d ", numbers[loc]);
                break;
            }
            else
            { // number in between
                printf("%03d,", numbers[loc]);
            }
        }
    }
}

주요 기능 호출

static void getNumWcommas (long long int n, int numbers[8])
{
    int i;
    int negative=0;
    if (n < 0)
    {
        negative = 1;
        n = -n;
    }
    for(i = 0; i<7; i++)
    {
        if (n < 1000)
        {
            numbers[i] = n;
            numbers[i+1] = -1;
            break;
        }
        numbers[i] = n%1000;
        n/=1000;
    }

    printNumber(numbers, i, negative);// non recursive print
}

테스트 출력

-9223372036854775807: -9,223,372,036,854,775,807
-1234567890         : -1,234,567,890
-123456             : -123,456
-12345              : -12,345
-1000               : -1,000
-999                : -999
-1                  : -1
0                   : 0
1                   : 1
999                 : 999
1000                : 1,000
12345               : 12,345
123456              : 123,456
1234567890          : 1,234,567,890
9223372036854775807 : 9,223,372,036,854,775,807

main () 클래스에서

int numberSeperated[8];
long long int number = 1234567890LL;
getNumWcommas(number, numberSeperated );

인쇄가 모두 필요한 int numberSeperated[8];경우 함수 내부 로 이동 getNumWcommas하여 이렇게 호출하십시오.getNumWcommas(number);


내 대답은 질문의 그림과 똑같은 결과를 형식화하지 않지만 경우에 따라 간단한 한 줄 또는 매크로로 실제 요구충족시킬 수 있습니다 . 필요에 따라 더 많은 천 그룹을 생성하도록 확장 할 수 있습니다.

결과는 다음과 같이 표시됩니다.

Value: 0'000'012'345

코드:

printf("Value: %llu'%03lu'%03lu'%03lu\n", (value / 1000 / 1000 / 1000), (value / 1000 / 1000) % 1000, (value / 1000) % 1000, value % 1000);

음수로 안전한 format_commas :

VS <2015는 snprintf를 구현하지 않기 때문에이 작업을 수행해야합니다.

#if defined(_WIN32)
    #define snprintf(buf,len, format,...) _snprintf_s(buf, len,len, format, __VA_ARGS__)
#endif

그리고

char* format_commas(int n, char *out)
{
    int c;
    char buf[100];
    char *p;
    char* q = out; // Backup pointer for return...

    if (n < 0)
    {
        *out++ = '-';
        n = abs(n);
    }


    snprintf(buf, 100, "%d", n);
    c = 2 - strlen(buf) % 3;

    for (p = buf; *p != 0; p++) {
        *out++ = *p;
        if (c == 1) {
            *out++ = '\'';
        }
        c = (c + 1) % 3;
    }
    *--out = 0;

    return q;
}

사용 예 :

size_t currentSize = getCurrentRSS();
size_t peakSize = getPeakRSS();


printf("Current size: %d\n", currentSize);
printf("Peak size: %d\n\n\n", peakSize);

char* szcurrentSize = (char*)malloc(100 * sizeof(char));
char* szpeakSize = (char*)malloc(100 * sizeof(char));

printf("Current size (f): %s\n", format_commas((int)currentSize, szcurrentSize));
printf("Peak size (f): %s\n", format_commas((int)currentSize, szpeakSize));

free(szcurrentSize);
free(szpeakSize);

저는 C 프로그래밍이 처음입니다. 여기 내 간단한 코드가 있습니다.

int main()
{
    //  1223 => 1,223
    int n;
    int a[10];
    printf(" n: ");
    scanf_s("%d", &n);
    int i = 0;
    while (n > 0)
    {
        int temp = n % 1000;
        a[i] = temp;
        n /= 1000;
        i++;
    }
    for (int j = i - 1; j >= 0; j--)
    {
        if (j == 0) 
        {
            printf("%d.", a[j]);
        }
        else printf("%d,",a[j]);
    }
    getch();
    return 0;
}

C에서이 작업을 수행하는 진짜 간단한 방법은 없습니다. int-to-string 함수를 수정하여 수행합니다.

void format_number(int n, char * out) {
    int i;
    int digit;
    int out_index = 0;

    for (i = n; i != 0; i /= 10) {
        digit = i % 10;

        if ((out_index + 1) % 4 == 0) {
            out[out_index++] = ',';
        }
        out[out_index++] = digit + '0';
    }
    out[out_index] = '\0';

    // then you reverse the out string as it was converted backwards (it's easier that way).
    // I'll let you figure that one out.
    strrev(out);
}

@paxdiablo 솔루션의 수정 된 버전이지만 WCHAR사용 wsprinf:

static WCHAR buffer[10];
static int pos = 0;

void printfcomma(const int &n) {
    if (n < 0) {
        wsprintf(buffer + pos, TEXT("-"));
        pos = lstrlen(buffer);
        printfcomma(-n);
        return;
    }
    if (n < 1000) {
        wsprintf(buffer + pos, TEXT("%d"), n);
        pos = lstrlen(buffer);
        return;
    }
    printfcomma(n / 1000);
    wsprintf(buffer + pos, TEXT(",%03d"), n % 1000);
    pos = lstrlen(buffer);
}

void my_sprintf(const int &n)
{
    pos = 0;
    printfcomma(n);
}

#include <stdio.h>

void punt(long long n){
    char s[28];
    int i = 27;
    if(n<0){n=-n; putchar('-');} 
    do{
        s[i--] = n%10 + '0';
        if(!(i%4) && n>9)s[i--]='.';
        n /= 10;
    }while(n);
    puts(&s[++i]);
}


int main(){
    punt(2134567890);
    punt(987);
    punt(9876);
    punt(-987);
    punt(-9876);
    punt(-654321);
    punt(0);
    punt(1000000000);
    punt(0x7FFFFFFFFFFFFFFF);
    punt(0x8000000000000001); // -max + 1 ...
}

내 솔루션은. 대신, 이것을 변경하는 것은 독자의 책임입니다.


아주 쉽게 할 수 있습니다 ...

//Make sure output buffer is big enough and that input is a valid null terminated string
void pretty_number(const char* input, char * output)
{
    int iInputLen = strlen(input);
    int iOutputBufferPos = 0;
    for(int i = 0; i < iInputLen; i++)
    {
        if((iInputLen-i) % 3 == 0 && i != 0)
        {
            output[iOutputBufferPos++] = ',';
        }

        output[iOutputBufferPos++] = input[i];
    }

    output[iOutputBufferPos] = '\0';
}

통화 예 :

char szBuffer[512];
pretty_number("1234567", szBuffer);
//strcmp(szBuffer, "1,234,567") == 0

void printfcomma ( long long unsigned int n) 
{

    char nstring[100];
     int m;
      int ptr;
       int i,j;


    sprintf(nstring,"%llu",n);
      m=strlen(nstring);

     ptr=m%3;
       if (ptr)
        {   for (i=0;i<ptr;i++)       // print first digits before comma
              printf("%c", nstring[i]); 
           printf(",");
         }
     j=0; 
     for (i=ptr;i<m;i++)      // print the rest inserting commas
          {
            printf("%c",nstring[i]);
            j++;
            if (j%3==0)
              if(i<(m-1)) printf(",");
           }

}

        // separate thousands
        int digit;
        int idx = 0;
        static char buffer[32];
        char* p = &buffer[32];

        *--p = '\0';
        for (int i = fCounter; i != 0; i /= 10)
        {
            digit = i % 10;

            if ((p - buffer) % 4 == 0)
                *--p = ' ';

            *--p = digit + '0';
        }

참고 URL : https://stackoverflow.com/questions/1449805/how-to-format-a-number-from-1123456789-to-1-123-456-789-in-c

반응형