Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- int main(void)
- {
- setlocale(LC_NUMERIC, "");
- printf("%'dn", 1123456789);
- return 0;
- }
- $ ./example
- 1,123,456,789
- 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);
- }
- #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
- 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);
- }
- }
- 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;
- }
- #include <stdio.h>
- #include <locale.h>
- int main()
- {
- int bignum=12345678;
- setlocale(LC_ALL,"");
- printf("Big number: %'dn",bignum);
- return 0;
- }
- #include <stdlib.h>
- #include <locale.h>
- #include <string.h>
- #define NUL '