www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Formating decimal numbers with commas (1,000.00)

reply jicman <cabrera_ _wrc.xerox.com> writes:
Greetings.

I have been trying, for more than an hour, to get information on how to format
a number (2342.23) to $2,342.23.  I can write a little function to do this, but
doesn't format already has this builtin?  I searched for vsprintf(), printf,
etc., and they all have a glyphic way of saying things.

I know that I can use,

real amt = 2342.23;
char[] z = format("%.2f",amt);

but, I want to do the $2,342.23.

Can anyone help a poor man? :-)

thanks,

josé
Sep 14 2010
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
On 14/09/2010 21:00, jicman wrote:
 Greetings.

 I have been trying, for more than an hour, to get information on how
 to format a number (2342.23) to $2,342.23.
Just wondering, where have you been searching for this information?
 I can write a little function to do this, but doesn't format already
 has this builtin?
<snip> If the documentation http://www.digitalmars.com/d/1.0/phobos/std_format.html is to go by, there doesn't seem to be any such feature, and a quick look through the code doesn't reveal one either. But there are many things std.format doesn't do. I'd imagine that someone's written a more powerful number formatting library module, but I don't know of it. Maybe that someone'll find this thread and enlighten us. Stewart.
Sep 14 2010
next sibling parent jicman <cabrera_ _wrc.xerox.com> writes:
Stewart Gordon Wrote:

 On 14/09/2010 21:00, jicman wrote:
 Greetings.

 I have been trying, for more than an hour, to get information on how
 to format a number (2342.23) to $2,342.23.
Just wondering, where have you been searching for this information?
Google gave me a bunch of hits, but none where useful. I can go back and get them for you, but all they have is the man pages or documentation on *print*. PHP does have nice little functions (format_number, format_currency, etc.) that provide the results I want. I was hoping that doFormat would allow somekind of formatting such as: doFormat("$%,.2f",1234.45); and return $1,234,45. or something like that. Adding the $ at the beginning is easy. .-) Or, doFormat("$10,.2f",1234.45); would return $ 1,234.56 where only 10 characters are allowed from the left side of the period back. Anyway, you get what I am saying. josé
Sep 14 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 14 Sep 2010 20:12:53 -0400, Stewart Gordon <smjg_1998 yahoo.com>  
wrote:

 On 14/09/2010 21:00, jicman wrote:
 Greetings.

 I have been trying, for more than an hour, to get information on how
 to format a number (2342.23) to $2,342.23.
Just wondering, where have you been searching for this information?
 I can write a little function to do this, but doesn't format already
 has this builtin?
<snip> If the documentation http://www.digitalmars.com/d/1.0/phobos/std_format.html is to go by, there doesn't seem to be any such feature, and a quick look through the code doesn't reveal one either. But there are many things std.format doesn't do. I'd imagine that someone's written a more powerful number formatting library module, but I don't know of it. Maybe that someone'll find this thread and enlighten us.
I believe Tango has an extensive locale library which does this sort of thing. -Steve
Sep 15 2010
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday, September 14, 2010 13:00:22 jicman wrote:
 Greetings.
=20
 I have been trying, for more than an hour, to get information on how to
 format a number (2342.23) to $2,342.23.  I can write a little function to
 do this, but doesn't format already has this builtin?  I searched for
 vsprintf(), printf, etc., and they all have a glyphic way of saying
 things.
=20
 I know that I can use,
=20
 real amt =3D 2342.23;
 char[] z =3D format("%.2f",amt);
=20
 but, I want to do the $2,342.23.
=20
 Can anyone help a poor man? :-)
=20
 thanks,
=20
 jos=E9
format() should work with the same types of parameters that printf() works = with=20 ( http://www.cplusplus.com/reference/clibrary/cstdio/printf/ ). That should= =20 allow you to set the precision that you want, but I don't believe that it d= oes=20 anything with commas. If you want that, I believe that you're going to have= to=20 code it yourself. =2D Jonathan M Davis
Sep 14 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Jonathan M Davis:
 If you want that, I believe that you're going to have to code it yourself.
It's a common need, I have it in my dlibs1, and I think that eventually it needs to be added to Phobos (so far none of my patches to Phobos I have put in Bugzila has being used, I don't know why, so I have lost part of the drive to write code for Phobos). Bye, bearophile
Sep 14 2010
next sibling parent reply jicman <cabrera_ _wrc.xerox.com> writes:
bearophile Wrote:

 Jonathan M Davis:
 If you want that, I believe that you're going to have to code it yourself.
It's a common need, I have it in my dlibs1, and I think that eventually it needs to be added to Phobos (so far none of my patches to Phobos I have put in Bugzila has being used, I don't know why, so I have lost part of the drive to write code for Phobos). Bye, bearophile
Where can I download your dlibs1 library? It would be nice to have people continue to update phobos. Not everyone uses tango.
Sep 14 2010
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday 14 September 2010 18:39:16 jicman wrote:
 bearophile Wrote:
 Jonathan M Davis:
 If you want that, I believe that you're going to have to code it
 yourself.
It's a common need, I have it in my dlibs1, and I think that eventually it needs to be added to Phobos (so far none of my patches to Phobos I have put in Bugzila has being used, I don't know why, so I have lost part of the drive to write code for Phobos). Bye, bearophile
Where can I download your dlibs1 library? It would be nice to have people continue to update phobos. Not everyone uses tango.
Phobos gets updated all the time. There's quite a bit of work being done on it actually. But there are only so many people with commit access, and they only have so much time. So, things only get done so fast - both the stuff that they're working on and how many patches that they're able to examine for possible inclusion. - Jonathan M Davis
Sep 14 2010
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
jicman:
 Where can I download your dlibs1 library?
This is D1 code, it's slow because I usually print only few numbers like this: string thousands(TyIntegral)(TyIntegral n, string separator="_") { static assert (IsType!(TyIntegral, byte, ubyte, short, ushort, int, uint, long, ulong), "thousands() accepts only a integral numeric argument."); string ns = toString(abs(n)).reverse; string[] parts; for (int i = 0; i < ns.length; i += 3) parts ~= ns[i .. (i+3) < length ? (i+3) : length]; return (n < 0 ? "-" : "") ~ parts.join(separator).reverse; } Bye, bearophile
Sep 14 2010
parent reply jicman <cabrera_ _wrc.xerox.com> writes:
bearophile Wrote:

 jicman:
 Where can I download your dlibs1 library?
This is D1 code, it's slow because I usually print only few numbers like this: string thousands(TyIntegral)(TyIntegral n, string separator="_") { static assert (IsType!(TyIntegral, byte, ubyte, short, ushort, int, uint, long, ulong), "thousands() accepts only a integral numeric argument."); string ns = toString(abs(n)).reverse; string[] parts; for (int i = 0; i < ns.length; i += 3) parts ~= ns[i .. (i+3) < length ? (i+3) : length]; return (n < 0 ? "-" : "") ~ parts.join(separator).reverse; }
Way over my head. :-) Thanks, though. josé
Sep 14 2010
parent bearophile <bearophileHUGS lycos.com> writes:
jicman:

 Way over my head. :-)  Thanks, though.
It's not that complex. Try to comment out most of the code and add some prints. IsType is a recursive template missing in Phobos2 too, it returns true if the first type is any of the following ones, you may remove it if you aren't able to write it. Bye, bearophile
Sep 15 2010
prev sibling parent jicman <cabrera_ _wrc.xerox.com> writes:
bearophile Wrote:

 Jonathan M Davis:
 If you want that, I believe that you're going to have to code it yourself.
It's a common need, I have it in my dlibs1, and I think that eventually it needs to be added to Phobos (so far none of my patches to Phobos I have put in Bugzila has being used, I don't know why, so I have lost part of the drive to write code for Phobos). Bye, bearophile
Where can I download your dlibs1 library? It would be nice to have people continue to update phobos. Not everyone uses tango.
Sep 14 2010