The time now is 09/21/08 - 14:23
Log in: Username: Password:
Search forums for:
  

simple coding question

Post new topic   Reply to topic
Author Message
TASB
Sir Postalot
Sir Postalot


Joined: 12 Oct 2002
Posts: 1104
Location: The Outback



PostPosted: 03/26/03 - 11:06    Post subject: simple coding question Reply with quote

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
RealPoor Master of Posts


Joined: 11 Oct 2002
Posts: 7044



PostPosted: 03/26/03 - 11:18    Post subject: Reply with quote

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
Sir Postalot


Joined: 12 Oct 2002
Posts: 1104
Location: The Outback



PostPosted: 03/26/03 - 11:21    Post subject: Reply with quote

Isn't there a format function or something? Or am I getting messed up with VB?
Back to top
sinrakin
RealPoor Master of Posts
RealPoor Master of Posts


Joined: 11 Oct 2002
Posts: 7044



PostPosted: 03/26/03 - 11:22    Post subject: Reply with quote

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
RealPoor Master of Posts


Joined: 11 Oct 2002
Posts: 7044



PostPosted: 03/26/03 - 11:25    Post subject: Reply with quote

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
Sir Postalot


Joined: 12 Oct 2002
Posts: 1104
Location: The Outback



PostPosted: 03/26/03 - 11:29    Post subject: Reply with quote

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
RealPoor Sensei


Joined: 12 Oct 2002
Posts: 1598



PostPosted: 03/26/03 - 11:50    Post subject: Reply with quote

ya, I'd do an sprintf into a string, then format the string by hand.
Back to top
Occulis
RealPoor Jedi
RealPoor Jedi


Joined: 11 Oct 2002
Posts: 13293
Location: Moral Relativity Central



PostPosted: 03/26/03 - 13:30    Post subject: Reply with quote

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
RealPoor Master of Posts


Joined: 15 Oct 2002
Posts: 6354



PostPosted: 03/26/03 - 19:02    Post subject: Reply with quote

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
RealPoor Master of Posts


Joined: 11 Oct 2002
Posts: 8752



PostPosted: 03/26/03 - 19:06    Post subject: Reply with quote

Pearl?

Back to top
compusmack
RealPoor Master of Posts
RealPoor Master of Posts


Joined: 15 Oct 2002
Posts: 6354



PostPosted: 03/26/03 - 19:29    Post subject: Reply with quote

Akronn wrote:
Pearl?



Well i see that zorro made his appearance in this thread.

You forgot something:
Back to top
Display posts from previous:   
Post new topic   Reply to topic
Page 1 of 1

Related topics:
IE question
Fishing question
LOTR Online Radio question
Diablo 2 question
Question about the Eve Online trail
Newest Firmware Question Linksys BEFSR41
tech question
Question for the faithful about prayer
one question
Quick question (converting audio from .wmv to a mp3 or wav)
GBA emulator question.
Winamp 5 question
D&D Online gameplay question
Question about healing in Guild Wars
Question about cloning in Fallen Earth
Question about LOTR Online instance
Age of Conan Sieges question
question about the EQ patcher
Question on LDoN points
PS2 question