C# .NET String.Format() 为一个数字在千位添加逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/105770/
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
.NET String.Format() to add commas in thousands place for a number
提问by Seibar
I want to add a comma in the thousands place for a number.
我想在千位上为一个数字添加一个逗号。
String.Format()
?
String.Format()
?
采纳答案by Seibar
String.Format("{0:n}", 1234); // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876
回答by Stephen Wrighton
String.Format("{0:#,###,###.##}", MyNumber)
That will give you commas at the relevant points.
这会给你在相关点逗号。
回答by Stephen Wrighton
Note that the value that you're formatting should be numeric. It doesn't look like it will take a string representation of a number and format is with commas.
请注意,您要格式化的值应该是数字。看起来它不会采用数字的字符串表示形式,并且格式为逗号。
回答by Stephen Wrighton
int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000
回答by p.campbell
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
回答by alchemical
I found this to be the simplest way:
我发现这是最简单的方法:
myInteger.ToString("N0")
回答by prabir
If you want culture specific, you might want to try this:
如果你想要特定文化,你可能想试试这个:
(19950000.0).ToString("N",new CultureInfo("en-US"))
= 19,950,000.00
(19950000.0).ToString("N",new CultureInfo("en-US"))
= 19,950,000.00
(19950000.0).ToString("N",new CultureInfo("is-IS"))
= 19.950.000,00
(19950000.0).ToString("N",new CultureInfo("is-IS"))
= 19.950.000,00
Note: Some cultures use ,
to mean decimal rather than .
so be careful.
注意:有些文化使用,
十进制表示而不是.
小心。
回答by 8 John Volante
The method I used to not worry anymore about cultures and potential formatting issues is that I formatted it as currency and took out the currency symbol afterwards.
我过去不再担心文化和潜在格式问题的方法是将其格式化为货币,然后取出货币符号。
if (decimal.TryParse(tblCell, out result))
if (decimal.TryParse(tblCell, out result))
{
formattedValue = result.ToString("C").Substring(1);
}
回答by cmujica
For example String.Format("{0:0,0}", 1);
returns 01, for me is not valid
例如String.Format("{0:0,0}", 1);
返回 01,对我来说是无效的
This works for me
这对我有用
19950000.ToString("#,#", CultureInfo.InvariantCulture));
output 19,950,000
产量 19,950,000
回答by CoderTao
Standard formats, with their related outputs,
标准格式及其相关输出,
Console.WriteLine("Standard Numeric Format Specifiers");
String s = String.Format("(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
- 1234, -1234.565F);
Console.WriteLine(s);
Example output (en-us culture):
示例输出(en-us 文化):
(C) Currency: . . . . . . . . (,234.00)
(D) Decimal:. . . . . . . . . -1234
(E) Scientific: . . . . . . . -1.234565E+003
(F) Fixed point:. . . . . . . -1234.57
(G) General:. . . . . . . . . -1234
(default):. . . . . . . . -1234 (default = 'G')
(N) Number: . . . . . . . . . -1,234.00
(P) Percent:. . . . . . . . . -123,456.50 %
(R) Round-trip: . . . . . . . -1234.565
(X) Hexadecimal:. . . . . . . FFFFFB2E