C# 如何用逗号格式化数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/699921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I format a number with commas?
提问by
int a = 10000000;
a.ToString();
How do I make the output?
我如何制作输出?
10,000,000
10,000,000
采纳答案by CMS
Try N0
for no decimal part:
尝试N0
没有小数部分:
string formatted = a.ToString("N0"); // 10,000,000
回答by lc.
a.ToString("N0")
a.ToString("N0")
See also: Standard Numeric Formatting Strings from MSDN
另请参阅:来自 MSDN 的标准数字格式字符串
回答by Mike
a.tostring("00,000,000")
a.tostring("00,000,000")
回答by Willy David Jr
You can also do String.Format:
你也可以做 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:
如果你有小数,同样的代码会输出2个小数位:
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);
回答by pistol-pete
A simpler String.Format option:
一个更简单的 String.Format 选项:
int a = 10000000;
String.Format("{0:n0}", a); //10,000,000