19

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#?

Community
  • 1
  • 1
Niraj Choubey
  • 3,942
  • 18
  • 58
  • 93

5 Answers5

28

For format options for Int32.ToString(), see standard format strings or custom format strings.

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.

You also do not need to specify a comma for every position. The fact that there is a comma in the format string means that the culture-specific grouping is used.

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

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
8
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
Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
6

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

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

elCapitano
  • 1,821
  • 3
  • 22
  • 42
6

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.

driis
  • 161,458
  • 45
  • 265
  • 341
  • This is what I have been using but now need to know if it rounds or just removes decimals when encountered and having trouble finding an answer. Do you know which it is and if so which is it? Thanks. – akaBase Jun 26 '19 at 15:07
1
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

Jos van Egmond
  • 2,370
  • 15
  • 19