views:

261

answers:

2

I am trying to convert numbers in the form 123456 to 123,456 which I find easy enough by using the .NET ToString() method with a custom format of N0.

However in one case of data I am getting strange formats whilst using this method.

In this case I am generating a DataTable dynamically for my input table to test the code.

DataTable dt = new DataTable();

dt.Columns.Add("Cases", typeof(Int32));

For example my data looks like this before formatting is applied.

--------------------------------------------
| Cases | 2495 | 3937 | 3207 | 4173 | 4265 |
--------------------------------------------

I would like it to look like this.

-------------------------------------------------
| Cases | 2,495 | 3,937 | 3,207 | 4,173 | 4,265 |
-------------------------------------------------

I try formatting it by using code like so.

output.Rows[r][c] = Convert.ToInt32(input.Rows[c - 1][r + 1]).ToString("N0");

(Note: The reason for the strange formatting is because I am taking a DataTable and transposing it to another DataTable.)

Using the above formatting I now get the following data returned.

--------------------------------------------------------------------------
| Cases | 01/02/0495 | 01/03/0937 | 01/03/0207 | 01/04/0173 | 01/04/0265 |
--------------------------------------------------------------------------

I then tried it with one decimal point using:

output.Rows[r][c] = Convert.ToInt32(input.Rows[c - 1][r + 1]).ToString("N01");

Which gave me the following result.

------------------------------------------------------
| Cases | 2495.0 | 3937.0 | 3207.0 | 4173.0 | 4265.0 |
------------------------------------------------------

This has now confused me because it is accepting the fact that the number can be formatted this way.

The next thing I tried was different ways of formatting strings like so.

output.Rows[r][c] = string.Format("{0:N0}", input.Rows[c - 1][r + 1]);
output.Rows[r][c] = Convert.ToInt32(input.Rows[c - 1][r + 1]).ToString("#,##0");

None of these give the number format how I would like either. Can anybody advise where I am going wrong? Is it so simple that I have missed the point?

+1  A: 

It looks to me simply that you've set the data-type of the columns in the DataTable incorrectly. Try making it explicit as an int (it looks like DateTime at the moment).

Also - the job of the DataTable is to hold data; not to disply it. You could store it pre-formatted as strings, I guess (as long as you tell it that the columns are strings) - but if you are storing it as int, then forget the display; as soon as you have run Convert.ToInt32 your job is over. How it appears is the job of the DataGridView (or whatever); tell that about "N0"...

Marc Gravell
Unfortunately that is not the case in this example because I am generating an input DataTable as an example to test the code. The field in this case is defined by dt.Columns.Add("Cases", typeof(Int32)); which I will update my question with.
Ian Roke
Is that the case? I am not able to hold formatted data in a DataTable? If that is the case then why have I been able to do so for other DataTables? What I am doing to taking unformatted data, formatting it then transposing it to a new DataTable. This is the first example that hasn't worked for me.
Ian Roke
Fine - but if you want to hold formatted data, then be sure to tell it that the columns are strings. Otherwise it might make (incorrect) assumptions...
Marc Gravell
Re edit; in which case, under the bonnet the column will be an int[]; so calling ToString(...) is just making work for it; it needs to parse it *back* to an int, and then display it later...
Marc Gravell
Thanks for the comments I will move formatting out of the DataTable and do it in the GridView etc. instead.
Ian Roke
A: 

Try using simply "N" as in ToString("N");

Sohnee
That works but doesn't give me the formatted number to zero decimal places.
Ian Roke
If your DataTable just contains integers, how are there any decimal places anyway?
Jon Skeet
@Jon Because when you use "N" it looks at the NumberFormatInfo.NumberDecimalSeparator Property which takes the value from the control panel regional and language settings of the computer and in my case it must be two decimal places.
Ian Roke