format number in C# [duplicate]

2020-08-10 07:27发布

Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number

How to format a number 1234567 into 1,234,567 in C#?

标签: c#
5条回答
祖国的老花朵
2楼-- · 2020-08-10 08:11
string formatted = string.Format("{0:##,#}", 123456789);

It depends on the culture of your computer. Some countries use commas, some countries use dots. On my computer the output was: 123.456.789

查看更多
Root(大扎)
3楼-- · 2020-08-10 08:13
var decimalValue = 1234567m; 
var value =  String.Format("{0:N}", decimalValue); // 1,234,567.00

or without cents

var value =  String.Format("{0:N0}", decimalValue); // 1,234,567
查看更多
劳资没心,怎么记你
4楼-- · 2020-08-10 08:20

For format options for Int32.ToString(), see here or here.

For example:

string s = myIntValue.ToString("#,##0");

The same format options can be use in a String.Format, as in

string s = String.Format("the number {0:#,##0}!", myIntValue);

Do note that the , in that format doesn't specify a "use a comma" but rather that the grouping character for the current culture should be used, in the culture-specific positions.

So you get "1 234 567 890" for pl-PL or "1,23,45,67,890" for hi-IN.

查看更多
一夜七次
5楼-- · 2020-08-10 08:21

Using your current locale's thousands separator:

int n = 1234567 ;
n.ToString("N0");

Or, use the overload to ToString, which takes the culture as a parameter.

查看更多
劫难
6楼-- · 2020-08-10 08:28

Try String.Format("{0:##,####,####}", 8958712551)

For Examples have a look at http://www.csharp-examples.net/string-format-double/

查看更多
登录 后发表回答