printf and formatting numeric strings 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 8 post ] 

 Relevant Pages 

1. Printf: Formatting numeric values

2. Formatting numeric strings

3. Format string as a parameter to printf()?

4. printf format string: how bad style is this?

5. printf -*s format string

6. Examples of format strings for scanf/printf ?

7. placing output of printf in memory: formatting strings

8. printf and variable length string format (asterisk)

9. Printf format strings

10. printf() format-string parser wanted!

11. printf Format string

12. Source to format strings like printf()

 

 
Powered by phpBB® Forum Software