Jump to page: 1 2
Thread overview
Formating decimal numbers with commas (1,000.00)
Sep 14, 2010
jicman
Sep 15, 2010
Stewart Gordon
Sep 15, 2010
jicman
Sep 15, 2010
Jonathan M Davis
Sep 15, 2010
bearophile
Sep 15, 2010
jicman
Sep 15, 2010
Jonathan M Davis
Sep 15, 2010
bearophile
Sep 15, 2010
jicman
Sep 15, 2010
bearophile
Sep 15, 2010
jicman
September 14, 2010
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é
September 15, 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.
September 15, 2010
On Tuesday, September 14, 2010 13:00:22 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.  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é

format() should work with the same types of parameters that printf() works with ( http://www.cplusplus.com/reference/clibrary/cstdio/printf/ ). That should allow you to set the precision that you want, but I don't believe that it does anything with commas. If you want that, I believe that you're going to have to code it yourself.

- Jonathan M Davis
September 15, 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
September 15, 2010
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é

September 15, 2010
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.

September 15, 2010
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
September 15, 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
September 15, 2010
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.

September 15, 2010
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é
« First   ‹ Prev
1 2