How do I format a number with commas?

房东的猫 提交于 2019-12-04 16:28:07

问题


int a = 10000000;
a.ToString();

How do I make the output?

10,000,000


回答1:


Try N0 for no decimal part:

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



回答2:


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



回答3:


a.ToString("N0")

See also: Standard Numeric Formatting Strings from MSDN




回答4:


A simpler String.Format option:

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



回答5:


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



来源:https://stackoverflow.com/questions/699921/how-do-i-format-a-number-with-commas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!