Tom Ritter
Tom Ritter

Reputation: 101400

Format a number with commas, but keep decimals

I'd like to group the digits in a double by thousands, but also output however number of decimals are actually in the number. I cannot figure out the format string.

 1000 => 1,000
 100000 => 100,000
 123.456 => 123.456
 100000.21 => 100,000.21
 100200.123456 => 100,200.123456

Disclaimers (it's not as straight forward as you think):

Upvotes: 16

Views: 17126

Answers (6)

Rich Newman
Rich Newman

Reputation: 21

An old thread, but none of the answers looks entirely satisfactory to me. Most of them turn zero into the empty string, including the one with the most upvotes.

I think a better solution is "#,##0.#################", which does at least show zeroes. It's still ugly though. There must be a better way.

double[] vals = new double[] { 0.0, 0.1234, -0.1234, 1.0, 
                               123456789.123456, 123456789.0, 
                               0.123456789123456 };
foreach (double val in vals) 
{ 
    Console.WriteLine(val + ": " + val.ToString("#,##0.#################")); 
}

Upvotes: 2

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

Try this one:

VB:

Dim vals() As Double = {1000, 100000, 123.456, 100000.21, 100200.123456}
For Each val As Double in vals
   Console.WriteLine(val.ToString("###,###.#########"))
Next val

C#:

double[] vals = new double[] {1000, 100000, 123.456, 100000.21, 100200.123456};
foreach (double val in vals)
{
    Console.WriteLine(val.ToString("###,###.#########"));
}

The problem is that there is no format to handle an arbitrary number of decimals. However, since you can only store a very limited precision, you can just use that and add a few more # characters as needed to cover it, assuming you're willing to let it cut off any insignificant 0's.

And the MSDN link you posted is the exact place you'd go to read how to do this.

Upvotes: 0

LeppyR64
LeppyR64

Reputation: 5359

This could be a little slow, but it will do what you want.

public static string FormatNumber(double num)
{
    string s = num.ToString();
    string result = "";

    if(s.IndexOf('.') != -1)
    {
        result = s.Substring(s.IndexOf('.'));
        s = s.Substring(0, s.IndexOf('.'));
    }

    while (s.Length > 3)
    {
        result = "," + s.Substring(s.Length - 3);
        s = s.Substring(0, s.Length - 3);
    }
    if (s.Length == 0)
        return result.Substring(1);
    return s + result;
}

Upvotes: 0

Aaron Fischer
Aaron Fischer

Reputation: 21231

You can create a custom format string .ToString("###,###.######") Custom Numeric Format Strings

Upvotes: 0

dragonjujo
dragonjujo

Reputation: 358

Try simply use "#,#", this adds the commas for thousands et al, but I don't know if it will keep the decimals, otherwise "#,###.####################", or any number of '#' symbols that you want after the decimal.

Upvotes: 1

James Curran
James Curran

Reputation: 103575

This appears to do exactly what you want:

public void Code(params string[] args)
{
    Print(1000);
    Print(100000);
    Print(123.456);
    Print(100000.21 );
    Print(100200.123456);
}

void Print(double n)
{
    Console.WriteLine("{0:###,###.#######}", n);
}

1,000
100,000
123.456
100,000.21
100,200.123456

Upvotes: 11

Related Questions