Solved

Converting an integer to a string with thousand separators

Posted on 2008-06-20
10
5,652 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

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.
In this post we will learn different types of Android Layout and some basics of an Android App.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

617 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