Format Number in C #

Possible duplicate:
.NET String.Format () add commas to thousands of places for numbers

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

+10
c #
source share
5 answers

The format options for Int32.ToString () see here or here .

For example:

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

String.Format can use the same format options as in

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

Please note that , in this format, do not specify "use a comma", but rather, you should use a grouping of characters for the current culture, in the provisions of culturally specific.

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

+18
source share

Using the current thousands locale delimiter:

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

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

+6
source share
 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 
+5
source share

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

For examples, see http://www.csharp-examples.net/string-format-double/

+2
source share
 string formatted = string.Format("{0:##,#}", 123456789); 

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

+1
source share

All Articles