|
|
| Author |
Message |
TASB
Sir Postalot

Joined: 12 Oct 2002 Posts: 1104
Location: The Outback
|
Posted: 03/26/03 - 11:06 Post subject: simple coding question
|
|
|
printf ("%.2f", 1234.56);
outputs 1234.56
how do you make it output as 1,234.56 ie insert the commas every 3 didgits?
|
|
|
Back to top
|
|
|
|
 |
sinrakin
RealPoor Master of Posts

Joined: 11 Oct 2002 Posts: 7044
|
Posted: 03/26/03 - 11:18 Post subject:
|
|
|
|
The simple answer is, you don't. Or, you code it yourself explicitely. It's barely possible that setting the thousands separator in locale.h might make it magically work, but I suspect the odds against that are about 1000 to 1.
|
|
|
Back to top
|
|
|
|
 |
TASB
Sir Postalot

Joined: 12 Oct 2002 Posts: 1104
Location: The Outback
|
Posted: 03/26/03 - 11:21 Post subject:
|
|
|
|
Isn't there a format function or something? Or am I getting messed up with VB?
|
|
|
Back to top
|
|
|
|
 |
sinrakin
RealPoor Master of Posts

Joined: 11 Oct 2002 Posts: 7044
|
Posted: 03/26/03 - 11:22 Post subject:
|
|
|
|
Nope, in 'C' you have to roll your own. Standard 'C' anyway - I've never used stuff like visual C - maybe they have some special routines.
|
|
|
Back to top
|
|
|
|
 |
sinrakin
RealPoor Master of Posts

Joined: 11 Oct 2002 Posts: 7044
|
Posted: 03/26/03 - 11:25 Post subject:
|
|
|
From the comp.lang.c FAQ:
| Quote: | 12.11: How can I print numbers with commas separating the thousands?
What about currency formatted numbers?
A: The functions in <locale.h> begin to provide some support for
these operations, but there is no standard routine for doing
either task. (The only thing printf() does in response to a
custom locale setting is to change its decimal-point character.)
References: ISO Sec. 7.4; H&S Sec. 11.6 pp. 301-4. |
|
|
|
Back to top
|
|
|
|
 |
TASB
Sir Postalot

Joined: 12 Oct 2002 Posts: 1104
Location: The Outback
|
Posted: 03/26/03 - 11:29 Post subject:
|
|
|
|
Thanks anyway. Hmm I guess the only easy work around would be to convert it to a string and then manipulate that. Not really worth it though.
|
|
|
Back to top
|
|
|
|
 |
lotek
RealPoor Sensei

Joined: 12 Oct 2002 Posts: 1598
|
Posted: 03/26/03 - 11:50 Post subject:
|
|
|
|
ya, I'd do an sprintf into a string, then format the string by hand.
|
|
|
Back to top
|
|
|
|
 |
Occulis
RealPoor Jedi

Joined: 11 Oct 2002 Posts: 13293
Location: Moral Relativity Central
|
Posted: 03/26/03 - 13:30 Post subject:
|
|
|
Check out the source to the CPAN module Number::Format and I'm sure you can see how they did it.
http://search.cpan.org/author/WRW/Number-Format-1.45/Format.pm
Also has these functions:
<pre>
THOUSANDS_SEP - character inserted between groups of 3 digits
DECIMAL_POINT - character separating integer and fractional parts
MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price
MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price
INT_CURR_SYMBOL - character(s) denoting currency (see format_price())
DECIMAL_DIGITS - number of digits to the right of dec point (def 2)
DECIMAL_FILL - boolean; whether to add zeroes to fill out decimal
NEG_FORMAT - format to display negative numbers (def ``-x'')
KILO_SUFFIX - suffix to add when format_bytes formats kilobytes
MEGA_SUFFIX - " " " " " " megabytes
GIGA_SUFFIX - " " " " " " gigabytes
</pre>
|
|
|
Back to top
|
|
|
|
 |
compusmack
RealPoor Master of Posts

Joined: 15 Oct 2002 Posts: 6354
|
Posted: 03/26/03 - 19:02 Post subject:
|
|
|
| Dunn wrote: | Check out the source to the CPAN module Number::Format and I'm sure you can see how they did it.
http://search.cpan.org/author/WRW/Number-Format-1.45/Format.pm
Also has these functions:
<pre>
THOUSANDS_SEP - character inserted between groups of 3 digits
DECIMAL_POINT - character separating integer and fractional parts
MON_THOUSANDS_SEP - like THOUSANDS_SEP, but used for format_price
MON_DECIMAL_POINT - like DECIMAL_POINT, but used for format_price
INT_CURR_SYMBOL - character(s) denoting currency (see format_price())
DECIMAL_DIGITS - number of digits to the right of dec point (def 2)
DECIMAL_FILL - boolean; whether to add zeroes to fill out decimal
NEG_FORMAT - format to display negative numbers (def ``-x'')
KILO_SUFFIX - suffix to add when format_bytes formats kilobytes
MEGA_SUFFIX - " " " " " " megabytes
GIGA_SUFFIX - " " " " " " gigabytes
</pre> |
Is that even C? looks like it might be perl or something.
|
|
|
Back to top
|
|
|
|
 |
Akronn
RealPoor Master of Posts

Joined: 11 Oct 2002 Posts: 8752
|
Posted: 03/26/03 - 19:06 Post subject:
|
|
|
Pearl?
|
|
|
Back to top
|
|
|
|
 |
compusmack
RealPoor Master of Posts

Joined: 15 Oct 2002 Posts: 6354
|
Posted: 03/26/03 - 19:29 Post subject:
|
|
|
| Akronn wrote: | Pearl?
 |
Well i see that zorro made his appearance in this thread.
You forgot something:
|
|
|
Back to top
|
|
|
|
 |
|
|