Formating decimal numbers with commas (1,000.00)

bearophile bearophileHUGS at lycos.com
Tue Sep 14 18:53:31 PDT 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


More information about the Digitalmars-d-learn mailing list