Hi,
How can I write a number 123456 formatted as 123,456 using printf?
Regards,
...ab 10 28555
On Mar 28, 8:50 am, abubak...@gmail .com wrote:
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); ab*******@gmail .com wrote:
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.
On Mar 28, 8:50 am, abubak...@gmail .com wrote:
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:ja****** ***@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
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...@gm ail.comwrote:
On Mar 28, 8:50 am, abubak...@gmail .com wrote:
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 objektorientier ter Datenverarbeitu ng
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
Please trim signatures before replying, and do not top-post. I have
fixed it below. ab*******@gmail .com wrote:
On Mar 28, 6:31 pm, "James Kanze" <james.ka...@gm ail.comwrote:
>On Mar 28, 8:50 am, abubak...@gmail .com wrote:
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<c har{
explicit my_facet(size_t refs = 0) : std::numpunct<c har>(refs) {}
virtual char do_thousands_se p() const { return ','; }
virtual std::string do_grouping() const { return "\003"; }
};
int main()
{
std::locale global;
std::locale withgroupings(g lobal, 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
On 27 Mar 2007 23:50:34 -0700, ab*******@gmail .com wrote in
comp.lang.c++:
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.l earn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
On Mar 29, 4:48 am, Jack Klein <jackkl...@spam cop.netwrote:
On 27 Mar 2007 23:50:34 -0700, abubak...@gmail .com wrote in
comp.lang.c++:
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".
oh, c'mon.. he was asking about 123456 :)
The correct conversion specifier is "%d,%03d".
On Mar 28, 3:46 pm, abubak...@gmail .com wrote:
And how do I set the locale of a ostream and use it too ?
ostream::imbue( ).
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Mar 29, 4:48 am, Jack Klein <jackkl...@spam cop.netwrote:
On 27 Mar 2007 23:50:34 -0700, abubak...@gmail .com wrote in
comp.lang.c++:
How can I write a number 123456 formatted as 123,456 using printf?
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.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matt |
last post by:
Hello,
Is there a means to use printf()-like formatting (eg:
http://www.mkssoftware.com/docs/man1/printf.1.asp ) using class string
and/or iostream constructs so that I need not have to define
fixed-size character buffers (like it appears that I have to do in
printf()...or maybe there's an alternative)?
-Matt
--
|
by: wongjoekmeu |
last post by:
Hello All,
I have a following piece of code in C that I need to rewrite to C++.
However I just don't seem to be able to understand the format rules in
C.
The C code look like this:
I have two doubles x and y which has a value. Now their values are
written to
|
by: Koster |
last post by:
Hi folks,
As I understand it, amongst other things, the comma operator may be used to
cause any number of expressions to be evaluated (who's results are thrown
away except the last) where only one is expected, without the use of braces.
So
if (condition) i = 0, j = 1;
causes both i = 0 and j = 1 expressions to be evaluated if condition...
|
by: Shawn Odekirk |
last post by:
Some code I have inherited contains a macro like the following:
#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal state\n"), TRUE) : \
(state = newstate, FALSE)
This macro is called like this:
setState(state, ST_Used);
|
by: jchludzinski |
last post by:
I tryin' to print out a string with a variable number of blanks/spaces
preceding the string. I know that if I use:
fprintf( stdout, "%12s", string );
I get 12 blanks preceding 'string'. If I use:
n = 12;
fprintf( stdout, "%(n)s: ", string );
| |
by: Martin Jørgensen |
last post by:
Hi,
Perhaps a stupid question but I wonder if this isn't possible using C:
printf("\nOr push <enter> for default (which is theta=%ld)%s", theta, ": ");
It would be nicer to have it on one line... (I know this works):
printf("\nOr push <enter> for default (which is theta=%ld)", theta);
|
by: Steven D'Aprano |
last post by:
I have a sinking feeling I'm missing something really,
really simple.
I'm looking for a format string similar to '%.3f'
except that trailing zeroes are not included.
To give some examples:
Float String
1.0 1
|
by: Neal Becker |
last post by:
I have a list of strings (sys.argv actually). I want to print them as a
space-delimited string (actually, the same way they went into the command
line, so I can cut and paste)
So if I run my program like:
../my_prog a b c d
I want it to print:
'./my_prog' 'a' 'b' 'c' 'd'
|
by: Annie Bender |
last post by:
Hi again everyone. Is there a standard formatting function available in VB to insert the 1000 comma separator in a calculated numeric result? In Excel, the same can be achieved using the standard function =TEXT(123456,"000\,000") to produce 123,456. If not, what would you suggest as coding for a customized function or sub? The formatted result...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |