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

How do I make the output?

10,000,000

bendewey
  • 38,870
  • 12
  • 96
  • 123
  • 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

5 Answers5

73

Try N0 for no decimal part:

string formatted = a.ToString("N0"); // 10,000,000
Christian C. Salvadó
  • 769,263
  • 179
  • 909
  • 832
  • 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
10

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
  • 7,817
  • 5
  • 40
  • 51
7

a.ToString("N0")

See also: Standard Numeric Formatting Strings from MSDN

lc.
  • 109,978
  • 20
  • 153
  • 183
2

A simpler String.Format option:

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

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

Mike
  • 5,013
  • 3
  • 24
  • 19