Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a number with commas?

People also ask

What is the comma in 1000?

The character used as the thousands separator In the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space.


Try N0 for no decimal part:

string formatted = a.ToString("N0"); // 10,000,000

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);

a.ToString("N0")

See also: Standard Numeric Formatting Strings from MSDN


A simpler String.Format option:

int a = 10000000;
String.Format("{0:n0}", a); //10,000,000