Solved

Converting an integer to a string with thousand separators

Posted on 2008-06-20
10
5,251 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
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…

770 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