49
int a = 10000000;
a.ToString();

How do I make the output?

10,000,000

bendewey
  • 39,709
  • 13
  • 100
  • 125
  • Possible duplicate of [.NET String.Format() to add commas in thousands place for a number](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – Michael Feb 16 '17 at 15:54

6 Answers6

87

Try N0 for no decimal part:

string formatted = a.ToString("N0"); // 10,000,000
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Is there a way to do this using a.ToString("#");? In my case I need the value to be blank on zero, but I need commas too - or should I just do it like a.ToString("#,###,###,###,###,###,###")? – James Nov 18 '14 at 19:59
  • You probably need to do do an if{}else{} block to deal with the zero. – Steve Woods May 05 '15 at 06:15
  • @cms how to achive this **`10,24,78,000`** – Meer Feb 16 '16 at 08:37
12

You can also do String.Format:

int x = 100000;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x); 
//Will output: 100,000

If you have decimal, the same code will output 2 decimal places:

double x = 100000.2333;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x); 
//Will output: 100,000.23

To make comma instead of decimal use this:

double x = 100000.2333;
string y = string.Empty;
y = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:#,##0.##}", x);
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
10

a.ToString("N0")

See also: Standard Numeric Formatting Strings from MSDN

lc.
  • 113,939
  • 20
  • 158
  • 187
2

A simpler String.Format option:

int a = 10000000;
String.Format("{0:n0}", a); //10,000,000
pistol-pete
  • 1,213
  • 17
  • 13
1

Even simpler for c#6 or higher:

string formatted = $"{a:N0}";
Max
  • 1,784
  • 2
  • 19
  • 26
  • Saving the people a search: https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting?redirectedfrom=MSDN – Jacksonkr Jul 31 '23 at 18:25
-3

a.tostring("00,000,000")

Mike
  • 5,181
  • 3
  • 25
  • 19