0

I'm trying to add comma separators to a number. I've tried the advice here: add commas using String.Format for number and and here: .NET String.Format() to add commas in thousands place for a number but I can't get it to work - they just return the number without commas. The code I'm using is here:

public static string addCommas(string cash) { return string.Format("{0:n0}", cash).ToString();
}

Where am I going wrong?

Thanks.


Update: Hi all, thanks for your help, but all of those methods are returning the same error: "error CS1502: The best overloaded method match for 'BishopFlemingFunctions.addCommas(int)' has some invalid arguments" (or variations therof depending on what number type I'm using.) Any ideas?

Community
  • 1
  • 1
Oli
  • 1
  • 1
  • 2
  • That error message suggests that you are calling the method with an `int` argument, while you showed a `string` argument. – Hans Kesting Nov 30 '10 at 12:17
  • Hi - yeah, that was after trying the methods below, not with my original code. – Oli Nov 30 '10 at 12:31

5 Answers5

1

Well, you are sending in a string. it looks like you want a currency back

  1. Why are you passing in a string to the method if it is a numeric value?
  2. String.Format will return a string so there is not need to .ToString() it again.
  3. {0:c} = Currency format if you do not want the $ then use {0:n}
  4. Not sure you have to but you may need to do an explicit conversion if you pass it in as a string to (decimal)cash

return String.Format("{0:c}", (decimal)cash);

or

return String.Format("{0:n}", (decimal)cash);

but i think it should be something like:

public static string addCommas(decimal cash)
{
return String.Format("{0:c}", cash);
}

but this is such a simple statement i do not see the logic in making it a method, if you method is one line, in most cases, its not a method.

GeekDrool
  • 11
  • 1
1

In order to apply number formatting you have to pass cash as a number type (int, double, float etc)

Adam Flanagan
  • 3,052
  • 23
  • 31
0

i have a method on my custom class to convert any numbers

public static string ConvertToThosandSepratedNumber(object number)
{
    string retValue = "";
    retValue = string.Format("{0:N0}", Convert.ToDecimal(number));
    return retValue;
}
mRizvandi
  • 983
  • 1
  • 8
  • 20
0

Here is a fairly efficient way to Add commas for thousands place, etc. It is written in VB.net. It does not work for negative numbers.

Public Function AddCommas(number As Integer) As String
    Dim s As String = number.ToString()
    Dim sb As New StringBuilder(16)
    Dim countHead As Integer = s.Length Mod 3
    If countHead = 0 Then countHead = 3
    sb.Append(s.Substring(0, countHead))

    For I As Integer = countHead To s.Length - 1 Step 3
        sb.Append(","c)
        sb.Append(s.Substring(I, 3))
    Next

    Return sb.ToString()
End Function
0

Note the cash parameter is of type double and the .## at the end of the formatted string for cents.

EDIT

Here is the code in its entirety:

static class Program {
    static void Main() {
        double d = 123456789.7845;
        string s = addCommas(d);
        System.Console.WriteLine(s);
    }

    public static string addCommas(double cash) {
        return string.Format("${0:#,###0.##}", cash);
    }
}

This prints "$123,456,789.78" to console. If you're getting

error CS1502: The best overloaded method match for 'addCommas(double)' has some invalid arguments

check to make sure that you're calling the function properly and that you're actually passing in the correct data type. I encourage you to copy/paste the code I have above and run it - BY ITSELF.

bitxwise
  • 3,534
  • 2
  • 17
  • 22
  • Hi all, thanks for your help, but all of those methods are returning the same error: "error CS1502: The best overloaded method match for 'BishopFlemingFunctions.addCommas(int)' has some invalid arguments" (or variations therof depending on what number type I'm using.) Any ideas? – Oli Nov 30 '10 at 12:08