digitalmars.D.learn - Formating decimal numbers with commas (1,000.00)
- jicman (9/9) Sep 14 2010 Greetings.
-
Stewart Gordon
(12/17)
Sep 14 2010
- jicman (10/18) Sep 14 2010 Google gave me a bunch of hits, but none where useful. I can go back an...
- Steven Schveighoffer (5/23) Sep 15 2010 I believe Tango has an extensive locale library which does this sort of ...
- Jonathan M Davis (11/31) Sep 14 2010 format() should work with the same types of parameters that printf() wor...
- bearophile (4/5) Sep 14 2010 It's a common need, I have it in my dlibs1, and I think that eventually ...
- jicman (2/9) Sep 14 2010 Where can I download your dlibs1 library? It would be nice to have peop...
- Jonathan M Davis (7/22) Sep 14 2010 Phobos gets updated all the time. There's quite a bit of work being done...
- bearophile (13/14) Sep 14 2010 This is D1 code, it's slow because I usually print only few numbers like...
- jicman (3/18) Sep 14 2010 Way over my head. :-) Thanks, though.
- bearophile (4/5) Sep 15 2010 It's not that complex. Try to comment out most of the code and add some ...
- jicman (2/9) Sep 14 2010 Where can I download your dlibs1 library? It would be nice to have peop...
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
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
Stewart Gordon Wrote:On 14/09/2010 21:00, jicman wrote: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é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?
Sep 14 2010
On Tue, 14 Sep 2010 20:12:53 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:On 14/09/2010 21:00, jicman wrote:I believe Tango has an extensive locale library which does this sort of thing. -SteveGreetings. 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.
Sep 15 2010
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=E9format() 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
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
bearophile Wrote:Jonathan M Davis:Where can I download your dlibs1 library? It would be nice to have people continue to update phobos. Not everyone uses tango.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
On Tuesday 14 September 2010 18:39:16 jicman wrote:bearophile Wrote: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 DavisJonathan M Davis:Where can I download your dlibs1 library? It would be nice to have people continue to update phobos. Not everyone uses tango.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
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
bearophile Wrote:jicman:Way over my head. :-) Thanks, though. josé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; }
Sep 14 2010
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
bearophile Wrote:Jonathan M Davis:Where can I download your dlibs1 library? It would be nice to have people continue to update phobos. Not everyone uses tango.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