Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 28th, 2007, 07:55 AM
abubakarm@gmail.com
Guest
 
Posts: n/a
Default printf comma formatting question

Hi,
How can I write a number 123456 formatted as 123,456 using printf?

Regards,

...ab

  #2  
Old March 28th, 2007, 01:05 PM
mliptak
Guest
 
Posts: n/a
Default Re: printf comma formatting question

On Mar 28, 8:50 am, abubak...@gmail.com wrote:
Quote:
Hi,
How can I write a number 123456 formatted as 123,456 using printf?
>
Regards,
>
..ab
like this?

int n = 123456;
printf("%d,%d", n / 1000, n % 1000);

  #3  
Old March 28th, 2007, 01:55 PM
Jacek Dziedzic
Guest
 
Posts: n/a
Default Re: printf comma formatting question

abubakarm@gmail.com wrote:
Quote:
Hi,
How can I write a number 123456 formatted as 123,456 using printf?
int d=123456;
printf("%d,%d",d/1000,d%1000);

Though if you intend to use it for numbers other than 123456,
you might want to use another power of 10 instead of 1000 and
make sure that the result of the modulo is positive.

HTH,
- J.

  #4  
Old March 28th, 2007, 02:35 PM
James Kanze
Guest
 
Posts: n/a
Default Re: printf comma formatting question

On Mar 28, 8:50 am, abubak...@gmail.com wrote:
Quote:
How can I write a number 123456 formatted as 123,456 using printf?
By setting an appropriate locale. With printf, you have to
modify the global locale to do this, with all of the problems
inherent in modifying global data. You'd be better off using
ostream, where you can set a locale specific to the stream.

--
James Kanze (GABI Software) mailto:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34

  #5  
Old March 28th, 2007, 02:55 PM
abubakarm@gmail.com
Guest
 
Posts: n/a
Default Re: printf comma formatting question

And how do I set the locale of a ostream and use it too ?

...ab

On Mar 28, 6:31 pm, "James Kanze" <james.ka...@gmail.comwrote:
Quote:
On Mar 28, 8:50 am, abubak...@gmail.com wrote:
>
Quote:
How can I write a number 123456 formatted as 123,456 using printf?
>
By setting an appropriate locale. With printf, you have to
modify the global locale to do this, with all of the problems
inherent in modifying global data. You'd be better off using
ostream, where you can set a locale specific to the stream.
>
--
James Kanze (GABI Software) mailto:james.ka...@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34

  #6  
Old March 28th, 2007, 04:45 PM
Marcus Kwok
Guest
 
Posts: n/a
Default Re: printf comma formatting question

Please trim signatures before replying, and do not top-post. I have
fixed it below.

abubakarm@gmail.com wrote:
Quote:
On Mar 28, 6:31 pm, "James Kanze" <james.ka...@gmail.comwrote:
Quote:
Quote:
>On Mar 28, 8:50 am, abubak...@gmail.com wrote:
Quote:
How can I write a number 123456 formatted as 123,456 using printf?
>>
>By setting an appropriate locale. With printf, you have to
>modify the global locale to do this, with all of the problems
>inherent in modifying global data. You'd be better off using
>ostream, where you can set a locale specific to the stream.
>
And how do I set the locale of a ostream and use it too ?
I found this example somewhere (I don't remember where, but probably
this newsgroup).


#include <iostream>
#include <locale>
#include <string>

struct my_facet : public std::numpunct<char{
explicit my_facet(size_t refs = 0) : std::numpunct<char>(refs) {}
virtual char do_thousands_sep() const { return ','; }
virtual std::string do_grouping() const { return "\003"; }
};

int main()
{
std::locale global;
std::locale withgroupings(global, new my_facet);
std::locale was = std::cout.imbue(withgroupings);
std::cout << 1000000 << std::endl;
std::cout.imbue(was);
return 0;
}


--
Marcus Kwok
Replace 'invalid' with 'net' to reply
  #7  
Old March 29th, 2007, 03:55 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: printf comma formatting question

On 27 Mar 2007 23:50:34 -0700, abubakarm@gmail.com wrote in
comp.lang.c++:
Quote:
Hi,
How can I write a number 123456 formatted as 123,456 using printf?
>
Regards,
Two people have given you an incorrect answer roughly corresponding to
this:

int x = 123456; // assumes int has 16 bits, not guaranteed
printf("%d,%d", x / 1000, x % 1000);

....which will give you "123,456".

But if 'x' is 123001, they will output the obviously incorrect:
"123, 1".

The correct conversion specifier is "%d,%03d".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  #8  
Old March 29th, 2007, 07:05 AM
mliptak
Guest
 
Posts: n/a
Default Re: printf comma formatting question

On Mar 29, 4:48 am, Jack Klein <jackkl...@spamcop.netwrote:
Quote:
On 27 Mar 2007 23:50:34 -0700, abubak...@gmail.com wrote in
comp.lang.c++:
>
Quote:
Hi,
How can I write a number 123456 formatted as 123,456 using printf?
>
Quote:
Regards,
>
Two people have given you an incorrect answer roughly corresponding to
this:
>
int x = 123456; // assumes int has 16 bits, not guaranteed
printf("%d,%d", x / 1000, x % 1000);
>
...which will give you "123,456".
>
But if 'x' is 123001, they will output the obviously incorrect:
"123, 1".
oh, c'mon.. he was asking about 123456 :)
Quote:
The correct conversion specifier is "%d,%03d".
>
  #9  
Old March 29th, 2007, 09:25 AM
James Kanze
Guest
 
Posts: n/a
Default Re: printf comma formatting question

On Mar 28, 3:46 pm, abubak...@gmail.com wrote:
Quote:
And how do I set the locale of a ostream and use it too ?
ostream::imbue().

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  #10  
Old March 29th, 2007, 09:25 AM
James Kanze
Guest
 
Posts: n/a
Default Re: printf comma formatting question

On Mar 29, 4:48 am, Jack Klein <jackkl...@spamcop.netwrote:
Quote:
On 27 Mar 2007 23:50:34 -0700, abubak...@gmail.com wrote in
comp.lang.c++:
Quote:
Quote:
How can I write a number 123456 formatted as 123,456 using printf?
Quote:
Two people have given you an incorrect answer roughly corresponding to
this:
Quote:
int x = 123456; // assumes int has 16 bits, not guaranteed
printf("%d,%d", x / 1000, x % 1000);
Quote:
...which will give you "123,456".
Quote:
But if 'x' is 123001, they will output the obviously incorrect:
"123, 1".
Quote:
The correct conversion specifier is "%d,%03d".
Which fails for 1234567.

The correct solution is to use the appropriate locale.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  #11  
Old March 29th, 2007, 09:25 AM
James Kanze
Guest
 
Posts: n/a
Default Re: printf comma formatting question

On Mar 29, 7:58 am, "mliptak" <Meht...@gmail.comwrote:
Quote:
On Mar 29, 4:48 am, Jack Klein <jackkl...@spamcop.netwrote:
Quote:
Quote:
On 27 Mar 2007 23:50:34 -0700, abubak...@gmail.com wrote in
comp.lang.c++:
Quote:
Quote:
Quote:
How can I write a number 123456 formatted as 123,456 using printf?
Quote:
Quote:
Two people have given you an incorrect answer roughly corresponding to
this:
Quote:
Quote:
int x = 123456; // assumes int has 16 bits, not guaranteed
printf("%d,%d", x / 1000, x % 1000);
Quote:
Quote:
...which will give you "123,456".
Quote:
Quote:
But if 'x' is 123001, they will output the obviously incorrect:
"123, 1".
Quote:
oh, c'mon.. he was asking about 123456 :)
In which case, the simplest solution is:

printf( "123,456" ) ;

But don't be stupid. it's obvious that if the only value he was
interested in was 123456, this solution would have occured to
him.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles