Solved

Converting an integer to a string with thousand separators

Posted on 2008-06-20
10
5,324 Views
Last Modified: 2008-06-22
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).
0
Comment
Question by:dedekind
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Assisted Solution

by:NeoTeq
NeoTeq earned 50 total points
ID: 21833065
This does what you are looking for.
int x = 1000000;
 
Console.WriteLine(x.ToString("#,##0"));

Open in new window

0
 

Author Comment

by:dedekind
ID: 21833215
Are you sure this takes into account the current system locale, i.e. displaying , when it should and . when it should?
0
 

Author Comment

by:dedekind
ID: 21833217
And also, if you could explain to me a bit WHY this would work it would be great.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:NeoTeq
ID: 21833316
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 Comment

by:dedekind
ID: 21834769
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 21835275
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21835284
sorry, there are extra closing parenthesis in my examples
0
 

Author Comment

by:dedekind
ID: 21839174
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 21841179
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question