-2

I have a bunch of data that I get as a string, and it needs to be formatted as a number (int).

So something like this: 128989899 needs to be displayed as 128,989,899

What would the correct way to achieve this?

PS: Currently existing questions do not address my question, before closing my question, please read it carefully or if you do close it, please provide a concise answer to what I am asking.

sarsnake
  • 26,667
  • 58
  • 180
  • 286
  • So to clarify, you receive `strings` containing data such as `128989899` and you wish to display this somewhere as a string, in the format `128,989,899`? – Martin Feb 24 '14 at 19:06
  • I have seen your previous question. What was wrong with the link suggested as duplicate and why you are now adding the VB warning? – varocarbas Feb 24 '14 at 19:06
  • None of the formats in the link suggested achieve what I need. – sarsnake Feb 24 '14 at 19:10
  • to Martin, yes. that is correct – sarsnake Feb 24 '14 at 19:10
  • 1
    @Bonifacio2, please tell me how your link applies to my question and what I asked wrong. I am genuinely interested. Because none of the questions addressed my issue as I demonstrated below. Did you read my question? Do you answer c# questions here? – sarsnake Feb 24 '14 at 19:27
  • 2
    There were enough clues for you to learn and derive the answers from the links provided. Also the point @Bonifacio2 made is not about who's right or wrong, it's about about educating ourselves on how to ask good questions and make the SO community better. – Chaitanya Feb 24 '14 at 19:32
  • Precisely, @Chaitanya. – Bonifacio2 Feb 24 '14 at 20:28
  • @Chaitanya, following your logic, stackoverflow should be shut down since there is MSDN already. No need for question and answer site since the info is already out there and there are "enough clues". To remind you: this site is supposed to answer the question asked not give you clues. I can find clues online w/o coming here. When I post here, I need code that works. If you can't provide that, move along. And that, Bonifacio2 applies to you first and foremost – sarsnake Feb 24 '14 at 20:37

3 Answers3

3

If I understand the question correctly, you have a string containing numbers stored as text.

You wish these to be displayed as comma-separated numbers such as 123,456,789.

Something along the lines of the following should help to achieve this, simply converting to an int and then back again into a string in the correct format:

string Input = "128989899";
int TempInt;
int.TryParse(Input, out TempInt);
string Output = TempInt.ToString("#,###");
Martin
  • 16,093
  • 1
  • 29
  • 48
2

Something like this:

var number = "128989899";
Console.WriteLine(String.Format("{0:N0}", Int32.Parse(number)));
Nick Gotch
  • 9,167
  • 14
  • 70
  • 97
  • @sarsnake note that this answer (very slight adapted) was also in the replicated link :) – varocarbas Feb 24 '14 at 19:13
  • no, it wasn't. It had {0:N} format, but not {0:N0} which achieves different result. The point of this site is to answer the question asked not the question you think was asked. – sarsnake Feb 24 '14 at 19:18
  • A small suggestion is to always use tryparse because it is more robust. – Chaitanya Feb 24 '14 at 19:20
  • @sarsnake As commented right now in your other post, any alternative (proposed here or in the other link) works fine. You just have to convert the string into a number, via Parse/TryParse or Convert.ToInt32. But this was implicit; you should have asked rather than posting a new question. – varocarbas Feb 24 '14 at 19:23
  • no. The alternative didn't work fine. Format mentioned in the alternative question gives me 128,989,899.00 and I need 128,989,899. – sarsnake Feb 24 '14 at 20:38
1

Duplicate: string format numbers

int number = 1000000000;
string commaseperated = number.ToString("#,##0");
Community
  • 1
  • 1
Chaitanya
  • 1,698
  • 5
  • 21
  • 41
  • Can you please READ my question carefully? I have data coming in as string (not int); I could of course, convert it to to int, but I am asking whether this could be achieved without doing so. – sarsnake Feb 24 '14 at 19:09
  • Yes, it COULD be achieve without converting to int... but it would be SO much easier and clear to convert to int then back to string. – Scottie Feb 24 '14 at 19:11
  • @sarsnake - But the answer you accepted also converts the string to int as well! – Chris Dunaway Feb 24 '14 at 23:03