0

I'm trying to put comma's between long numbers automatically, but so far without success. I'm probably making a very simple mistake, but so far I can't figure it out. This is the code I currently have, but for some reason I'm getting 123456789 as the output.

    string s = "123456789";
    string.Format("{0:#,###0}", s);
    MessageBox.Show(s); // Needs to output 123,456,789
Annoying Bot
  • 359
  • 1
  • 5
  • 13
  • 4
    You're missing the `{`, but also could just use `N` for general number format. Also, your input is a string, not a number. – Kent Boogaart Jun 05 '13 at 13:20
  • If you look at each character in the format string, can you tell me what each does? – Gabe Jun 05 '13 at 13:21
  • I think you need to add a curly bracket first of all. – Chris Spicer Jun 05 '13 at 13:21
  • I have no idea what's going on, but removing the brackets is not the solution. – Annoying Bot Jun 05 '13 at 13:23
  • 1
    its already posted, here is few [links](http://stackoverflow.com/questions/2545633/add-commas-using-string-format-for-number-and) and [here](http://stackoverflow.com/questions/699921/how-do-i-format-a-number-with-commas) I am sure it will help – Rajeev Bera Jun 05 '13 at 13:24

5 Answers5

1

Try this:

string value = string.Format("{0:#,###0}", 123456789);

In your code you are missing the initial { in the format string, and then number formatting options apply to numbers, while your s is a string.
You could convert the string to a number with int.Parse:

int s = int.Parse("123456789");
string value = string.Format("{0:#,###0}", 123456789);
MessageBox.Show(value); 
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
1

This should work (you need to pass String.Format() a number, not another String):

Int32 i = 123456789;
String s = String.Format("{0:#,###0}", i);
MessageBox.Show(s);

But consider the format string you're using...there are cleaner options available, as others are suggesting.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • This worked. The input is always a string though, because it's needs to be loaded from an external source. I converted it to Int32, used your code, and then converted it back to a string. This worked. I will try to make it cleaner, but still your help is much appreciated! – Annoying Bot Jun 05 '13 at 13:28
1
var input = 123456789;

// these two lines amount to the same thing
Console.WriteLine(input.ToString("N0"));
Console.WriteLine(string.Format("{0:N0}", input));

If, as per your question, you need to start with a string:

var stringInput = "123456789";
var input = int.Parse(stringInput);

// these two lines amount to the same thing
Console.WriteLine(input.ToString("N0"));
Console.WriteLine(string.Format("{0:N0}", input));

You'll possibly also need to take culture into account when parsing/formatting. See the overloads that take an IFormatProvider.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
0

Look at the number formatting information on MSDN: Standard Numeric Format Strings, or optionally at the custom format strings: Custom Numeric Format Strings.

For custom number formats:

The "," character serves as both a group separator and a number scaling specifier.

double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890      
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235   
w5l
  • 5,341
  • 1
  • 25
  • 43
0

There is so much wrong with your code, that's it's hard to describe every detail.

Look at this example:

namespace ConsoleApplication1
{
  using System;

  public class Program
  {
    public static void Main()
    {
      const int Number = 123456789;
      var formatted = string.Format("{0:#,###0}", Number);

      Console.WriteLine(formatted);
      Console.ReadLine();
    }
  }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142