Author |
Message |
David Crayfor #1 / 8
|
 printf and formatting numeric strings
Is there a feature of printf that can format a numeric picture string with comma seperators ie. int num = 32767; printf("number is %d\n", num ); What I want is "number is 32,767". I know I can crank it by hand, but am I missing something ?
|
Tue, 16 Nov 2004 23:37:41 GMT |
|
 |
Ben Pfaf #2 / 8
|
 printf and formatting numeric strings
Quote:
> Is there a feature of printf that can format a numeric picture string with > comma seperators
Nope. You'll have to postprocess the output yourself. -- "IMO, Perl is an excellent language to break your teeth on" --Micah Cowan
|
Wed, 17 Nov 2004 00:04:31 GMT |
|
 |
Eric Go #3 / 8
|
 printf and formatting numeric strings
Quote:
> Is there a feature of printf that can format a numeric picture string with > comma seperators
No.
|
Wed, 17 Nov 2004 00:12:03 GMT |
|
 |
HG #4 / 8
|
 printf and formatting numeric strings
Quote:
> Is there a feature of printf that can format a numeric picture string with > comma seperators > ie. > int num = 32767; > printf("number is %d\n", num ); > What I want is "number is 32,767". I know I can crank it by hand, but am I > missing something ?
Hi David, As far as I know, there is nothing within the standard that supports this, BUT if you happened to be on a linux system you can use the following: I must warn you that this is VERY system dependent and not very portable. YMMV. =) <off-topic> #include <locale.h> int main(void) { int num = 32767; ... setlocale (LC_ALL, "en_US"); printf("number is %'d\n", num ); /* Note of the extra comma added */ ... Quote: }
</off-topic> -HG
|
Wed, 17 Nov 2004 00:59:20 GMT |
|
 |
David Crayfor #5 / 8
|
 printf and formatting numeric strings
Quote: > printf("number is %'d\n", num ); /* Note of the extra comma added */
Thanks HG, I'm on an Z/OS and although the ' flag is documented, it's only for UNIX and Linux/390. Oh bollocks... Cheers for you help...
|
Wed, 17 Nov 2004 00:42:37 GMT |
|
 |
those who know me have no need of my nam #6 / 8
|
 printf and formatting numeric strings
in comp.lang.c i read: Quote: >printf("number is %d\n", num ); >What I want is "number is 32,767". I know I can crank it by hand, but am I >missing something ?
yes, you forgot to read the faq (#12.11). also you forgot to lurk for a while before posting. either of which would have answered your question. -- bringing you boring signatures for 17 years
|
Thu, 18 Nov 2004 06:23:31 GMT |
|
 |
Zoran Cutur #7 / 8
|
 printf and formatting numeric strings
Quote: > Is there a feature of printf that can format a numeric picture string with > comma seperators > ie. > int num = 32767; > printf("number is %d\n", num ); > What I want is "number is 32,767". I know I can crank it by hand, but am I > missing something ?
There certainly isn't a special specifier for this, but if you set your locale appropriately printf will use this locale to produce numbers as the one you request for. There is a field called thousands_sep in struct lconv. Lookup setlocale() and localeconv() for further information. --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Fri, 19 Nov 2004 14:57:26 GMT |
|
 |
Dan P #8 / 8
|
 printf and formatting numeric strings
Quote:
>> Is there a feature of printf that can format a numeric picture string with >> comma seperators >> ie. >> int num = 32767; >> printf("number is %d\n", num ); >> What I want is "number is 32,767". I know I can crank it by hand, but am I >> missing something ? >There certainly isn't a special specifier for this, but if you >set your locale appropriately printf will use this locale to
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Quote: >produce numbers as the one you request for.
Chapter and verse, please. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Fri, 19 Nov 2004 22:45:44 GMT |
|
|