0

Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number

I am trying to add commas to a number for the presentation layer and need to cast and then split the number on every third character in order to join on a ','.

So if i have a string like this

546546555

desired output:

546,546,555

Other times, the number could be longer or shorter:

254654

desired output:

254,654

Is it possible to split in this manner then join with a comma?

tahnks!

EDIT:

Hi Everyone,

Thanks very much for your help.

To add to this post I also found a way to do this in SQL:

SUBSTRING(CONVERT(varchar, CAST(NumItems AS money), 1), 0, LEN(CONVERT(varchar, CAST(NumDocs AS money), 1)) - 2) as [NumDocs]

Community
  • 1
  • 1
some_bloody_fool
  • 4,605
  • 14
  • 37
  • 46

5 Answers5

2

Rather than splitting the string manually, you should convert it to a number (or leave it as a number), and call the ToString method with the appropriate formatting:

Example

int value = 546546555;
string displayValue = value.ToString("#,#");

See this MSDN page for different format values:

C - Currency format

D - Decimal format

E - Scientific format

F - Fixed point format

G - General format

N - Number format

P - Percent format

R - Round trip format

X - Hexadecimal format

Dan
  • 9,717
  • 4
  • 47
  • 65
0

You should do this by converting your string to an integer, using Parse or ideally TryParse and use string formatting to display it:

var str = "546546555";
var formatted = String.Empty;
int value = 0;
if(int.TryParse(str,out value))
{
   formatted = value.ToString("#,#");
}

Live example: http://rextester.com/FHO11833

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

Assuming you aren't only trying to output numbers, here's a quick function that I believe would do what you are after:

string splitter(string tosplit, int num, string splitstring)
    {
        string output = "";
        for (int i = 0; i < tosplit.Length; i += num)
            if (i + num < tosplit.Length)
                output += tosplit.Substring(i, num) + ",";
            else
                output += tosplit.Substring(i);
        return output;
    }

Here, the output of splitter("546546555", 3, ",") would be 546,546,555

This would not be ideal for numbers though, as the other answers would cover this case perfectly.

Lyise
  • 1,110
  • 2
  • 12
  • 20
0

Not very good code, but it works.

public static string GetString(string val, int number)
        {
            List<string> res = new List<string>();
            res.Add("");

            int counter = 0, i = 0;
            while (i < val.Length)
            {
                while (res[counter].Length < number && i < val.Length)
                {
                    res[counter] += val[i];
                    i++;
                }
                res.Add("");
                counter++;
            }

            return string.Join(",", res.Where(r => !string.IsNullOrEmpty(r)));
        }

val - your input string number - number of characters you want to split, equals to 3 in your case

Vytalyi
  • 1,637
  • 3
  • 20
  • 33
0

Gene S and Dan seem to have the answer IMHO. The nice thing about using the built in formatting is that you can write localizable code. For example, the "," is the numeric group separator in the US, but the "." is used in Spain.

        var val = 12345678;
        CultureInfo c = CultureInfo.CurrentCulture;
        Application.CurrentCulture = new CultureInfo("EN-us");
        var s = String.Format("{0:#,#}", val);
        Application.CurrentCulture = new CultureInfo("ES-es");
        var i = String.Format("{0:#,#}", val);
        Application.CurrentCulture = c;
Les
  • 10,335
  • 4
  • 40
  • 60