printf comma formatting question

A

abubakarm

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

Regards,

...ab
 
J

Jacek Dziedzic

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.
 
J

James Kanze

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.
 
M

Marcus Kwok

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

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;
}
 
J

Jack Klein

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
 
M

mliptak

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 :)
 
J

James Kanze

On 27 Mar 2007 23:50:34 -0700, (e-mail address removed) wrote in
comp.lang.c++:
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".

Which fails for 1234567.

The correct solution is to use the appropriate locale.
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top