Solved

Converting an integer to a string with thousand separators

Posted on 2008-06-20
10
5,095 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now