# Converting an integer to a string with thousand separators

Let's say I have the integer (or better yet, long) 1000000. How can I convert it properly to a string with thousand separators?

On systems where comma is the thousand separator the outcome should be "1,000,000" and on systems where dot is the thousand separator the outcome should be "1.000.000".

I have tried doing String.Format("{0:n}", 1000000); but this just keeps giving me "1.000.000,00" which is not what I want (I don't want the decimal point there at the end, because I am dealing with integers and not doubles).
###### Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x

Software ArchitectCommented:
will be easier to use "N"

long x = 123456789;
string s = x.ToString("N");
or
string s = string.Format("{0:N}", x);

also, you can put specific culture information:

s = x.ToString("N", CultureInfo.InvariantCulture));  // will use , as thousand separator and . as decimal point
s = x.ToString("N", CultureInfo.CreateSpecificCulture("fr-FR")));   // inverse use of . and ,

also you can specify decimals, as:
s = x.ToString("N2");   // will show 2 decimal digits
0

Commented:
This does what you are looking for.
``````int x = 1000000;

Console.WriteLine(x.ToString("#,##0"));
``````
0

Author Commented:
Are you sure this takes into account the current system locale, i.e. displaying , when it should and . when it should?
0

Author Commented:
And also, if you could explain to me a bit WHY this would work it would be great.
0

Commented:
Yes, it takes system locale into account. As for an explanation why it works like this... It's custom number formatting. A quick google turns up this page:

http://blog.stevex.net/index.php/string-formatting-in-csharp/

Have a look at the chapter 'Numbers', specifically 'Custom number formatting'.
0

Author Commented:
I still find this a mystery. That SteveX guy has a nice website there, but he still leaves me in the dark. For instance, why does the format string "#,0" also seem to work fine in this case? I guess I know theoretically from that website what # represents, what "," represents and what 0 represents, but I don't know how to put all them together and understand the result.

I have increased the points of this question to 300.
0

Software ArchitectCommented:
sorry, there are extra closing parenthesis in my examples
0

Author Commented:
I found the "N" solution to be the simplest to understand. In my case therefore I ended up using simply "N0" (N zero). So thanks for this advice and all the other advices.

Just out of curiosity though, what comments were said that were deleted? I didn't get a chance to see them before they got removed.
0

Commented:
dedekind,

You asked "For instance, why does the format string "#,0" also seem to work fine in this case?"

The answer is in the documentation.

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

The ',' character serves as both a thousand separator specifier and a number scaling specifier.

Thousand separator specifier: If one or more ',' characters is specified between two digit placeholders (0 or #) that format the integral digits of a number, a group separator character is inserted between each number group in the integral part of the output.
0