Formating decimal numbers with commas (1,000.00)

jicman cabrera_ at _wrc.xerox.com
Tue Sep 14 21:35:45 PDT 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é


More information about the Digitalmars-d-learn mailing list