Sei*_*bar 1140
String.Format("{0:n}", 1234); // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876
Run Code Online (Sandbox Code Playgroud)
alc*_*cal 361
我发现这是最简单的方法:
myInteger.ToString("N0")
Run Code Online (Sandbox Code Playgroud)
小智 144
int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000
Run Code Online (Sandbox Code Playgroud)
pra*_*bir 96
如果您想要特定于文化,您可能想尝试这样:
(19950000.0).ToString("N",new CultureInfo("en-US"))
= 19,950,000.00
(19950000.0).ToString("N",new CultureInfo("is-IS"))
= 19.950.000,00
注意:有些文化,
用来表示十进制而不是.
小心.
Cod*_*Tao 72
标准格式及其相关输出,
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);
Run Code Online (Sandbox Code Playgroud)
示例输出(en-us culture):
(C) Currency: . . . . . . . . ($1,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
Run Code Online (Sandbox Code Playgroud)
Den*_*nis 39
这是最好的格式.适用于所有这些情况:
String.Format( "{0:#,##0.##}", 0 ); // 0
String.Format( "{0:#,##0.##}", 0.5 ); // 0.5 - some of the formats above fail here.
String.Format( "{0:#,##0.##}", 12314 ); // 12,314
String.Format( "{0:#,##0.##}", 12314.23123 ); // 12,314.23
String.Format( "{0:#,##0.##}", 12314.2 ); // 12,314.2
String.Format( "{0:#,##0.##}", 1231412314.2 ); // 1,231,412,314.2
Run Code Online (Sandbox Code Playgroud)
Ste*_*ton 34
String.Format("{0:#,###,###.##}", MyNumber)
Run Code Online (Sandbox Code Playgroud)
那将在相关点给你逗号.
von*_* v. 32
投票最多的答案很棒,并且有效期约7年.随着C#6.0的引入,特别是字符串插值,有一个整洁的,IMO更安全的方式来做所谓的问题to add commas in thousands place for a number
:
var i = 5222000;
var s = $"{i:n} is the number"; // results to > 5,222,000.00 is the number
s = $"{i:n0} has no decimal"; // results to > 5,222,000 has no decimal
Run Code Online (Sandbox Code Playgroud)
变量i
放在占位符(即{0}
)的位置.所以没有必要记住哪个对象到达哪个位置.格式(即:n
)没有改变.有关新功能的完整功能,您可以转到此页面.
a_m*_*dev 26
这很简单:
float num = 23658; // for example
num = num.ToString("N0"); // Returns 23,658
Run Code Online (Sandbox Code Playgroud)
更多信息在这里
Rav*_*sai 21
如果您希望强制使用","分隔符而不考虑文化(例如在跟踪或日志消息中),则以下代码将起作用并且具有额外的好处,即告诉下一个发现它的人正是您正在做的事情.
int integerValue = 19400320;
string formatted = string.Format(CultureInfo.InvariantCulture, "{0:N0}", integerValue);
Run Code Online (Sandbox Code Playgroud)
集格式为"19,400,320"
Yit*_*erg 20
以下示例显示使用包含零占位符的自定义格式字符串格式化的多个值.
String.Format("{0:N1}", 29255.0);
Run Code Online (Sandbox Code Playgroud)
要么
29255.0.ToString("N1")
Run Code Online (Sandbox Code Playgroud)
结果"29,255.0"
String.Format("{0:N2}", 29255.0);
Run Code Online (Sandbox Code Playgroud)
要么
29255.0.ToString("N2")
Run Code Online (Sandbox Code Playgroud)
结果"29,255.00"
Mar*_*sky 17
C# 7.1(也许更早?)通过字符串插值使这一切变得简单而美观:
var jackpot = 1_000_000; // underscore separators in numeric literals also available since C# 7.0
var niceNumberString = $"Jackpot is {jackpot:n}";
var niceMoneyString = $"Jackpot is {jackpot:C}";
Run Code Online (Sandbox Code Playgroud)
p.c*_*ell 11
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
Run Code Online (Sandbox Code Playgroud)
bra*_*roo 11
更简单,使用字符串插值而不是String.Format
$"{12456:n0}"; // 12,456
$"{12456:n2}"; // 12,456.00
Run Code Online (Sandbox Code Playgroud)
或使用yourVariable
double yourVariable = 12456.0;
$"{yourVariable:n0}";
$"{yourVariable:n2}";
Run Code Online (Sandbox Code Playgroud)
小智 9
例如String.Format("{0:0,0}", 1);
返回01,对我来说无效
这适合我
19950000.ToString("#,#", CultureInfo.InvariantCulture));
Run Code Online (Sandbox Code Playgroud)
产量19,950,000
小智 5
String.Format("0,###.###"); also works with decimal places
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
670179 次 |
最近记录: |