Commas in 'c' printf
Kurt Wall
kwall
Mon May 17 11:55:39 PDT 2004
Consuming 0.5K bytes, Michael Hipp blathered:
> It's been a long time since I programmed anything with financial-style
> numbers in c, but I thought there was a printf option to put commas in
> the numbers so you get 1,234,567 instead of the hard-to-read 1234567.
>
> Is there a simple c way to do this or do I have to write it?
You mean something like:
#include <stdio.h>
#include <locale.h>
int main(void)
{
char *locale;
locale = setlocale(LC_NUMERIC, "en_US.iso88591");
if (locale == NULL) {
perror("setlocale");
} else {
printf("%'d\n", 12345);
setlocale(LC_NUMERIC, locale);
}
return 0;
}
$ ./a.out
12,345
$
See setlocale(3), printf(3), locale(1), locale(5), and locale(7).
Note that the single quote between % and d is correct...
Kurt
--
Anything is good and useful if it's made of chocolate.
More information about the Linux-users
mailing list