0

I'm using this print statement:

printf(
 "%5.2lf %'15llu %'13llu %5.2lf %'15llu "
 "%'11u %'11u %'11u %'11u %'11u %'11u %'11u %'11u %'11u %'11u "
 "%'11u %'9u %'11u %'9u\n",
 (p->cbDecomp) ? 100.0 * ((double)p->cbComp)/p->cbDecomp : 0, p->cbDecomp, p->cbComp, 
 (p->cbDecompDup) ? 100.0 * ((double)p->cbComp)/p->cbDecompDup : 0, p->cbDecompDup, 
 p->cbIndFile, p->cbUdhFile, p->cUid, p->cFreeUid, p->cDups, cbUid, cbUdh, p->cTbl, p->cTblDel, cbTbl,
 p->cTblBuckets, p->cMaxTblInBucket, p->cUdhBuckets, p->cMaxUdhInBucket);

But getting this output:

  atp 001  0.39      1094017024       4281732  0.39      1110155264     9111191     7510837      267094           0        3940     1335470     3205128      352190           0     3521900      131072        13      131072        11
  atp 002  0.33        40898560        133896  0.33        40935424      355600      253716        9985           0           9       49925      119820       15297           0      152970        4096        13        4096         9
  atp 003  0.38       690307072       2640414  0.38       694116352     5763359     4680853      168532           0         930      842660     2022384      237391           0     2373910       65536        15       65536        11
  atp 004  0.70       661450752       4613727  0.69       667181056     5614000     6574177      161487           0        1399      807435     1937844      277030           0     2770300       16384        33        8192        42

The commas separating the thousands groups are not showing up! I think it has something to do with locale, but I don't know anything about locale.

[c698174@shldvgfas023] $ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

[c698174@shldvgfas023] $ cat /proc/version 
Linux version 2.6.32-236.el6.sf599499.x86_64 (mockbuild@x86-012.build.bos.redhat.com) (gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) ) #1 SMP Thu May 10 12:52:20 EDT 2012

Does anyone know how to get my commas???

johnnycrash
  • 5,184
  • 5
  • 34
  • 58

2 Answers2

2

Set your locale first.

#include <stdio.h>
#include <locale.h>
int main(int argc, char *argv[])
{
    printf("%'d\n", 1000000);
    setlocale(LC_ALL, "");
    printf("%'d\n", 1000000);
    return 0;
}

Output:

$ ./a.out 
1000000
1,000,000

The setlocale(LC_ALL, "") call will set the current locale according to the environment variables. The thousands separator is locale-specific.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0

After more staring I understood. Try this:

setlocale(LC_NUMERIC, "en_US.iso88591");
printf("%'15d\n", i);

This switches the C locale from the C default to the C numeric.

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45
  • Hard-coded locale choices never belong in programs; you should use the values from the environment. Instead, use `setlocale(LC_NUMERIC, "");` Forcing a legacy 8859 locale instead of a UTF-8 one is even worse... – R.. GitHub STOP HELPING ICE Oct 27 '12 at 01:11
  • @R I understand, but just out of curiosity, what are some of the reasons I shouldn't do this? – Florin Stingaciu Oct 27 '12 at 01:14
  • @FlorinStingaciu: It can cause garbage to be printed on the terminal if the encoding is set wrong. – Dietrich Epp Oct 27 '12 at 01:16
  • @DietrichEpp Thanks. [This](http://www.joelonsoftware.com/articles/Unicode.html) keeps popping up through my readings, maybe its time I actually read it. – Florin Stingaciu Oct 27 '12 at 01:19
  • (1) Available locale names are system-specific. Not just OS, actual local system - many users will not build any locales but the one they want to use! (2) Even if the locale does exist, it's likely (as in your code) to have the wrong character encoding, and thus not interact correctly with the user's input, local files, filenames, etc. (3) There's a perfectly good way to request the user's preference, using `""` as the requested locale. – R.. GitHub STOP HELPING ICE Oct 27 '12 at 02:53