0

I have the same question as in Objective C display money format like Sensible Soccer but for C#. Is there any special parameters for .ToString() method, or some library which can format currency in such case? Thanks

Community
  • 1
  • 1
user1947702
  • 154
  • 1
  • 3
  • 12
  • There is no format like that on [Standard Numeric Format Strings](http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx) list. – MarcinJuraszek Feb 27 '14 at 20:32

2 Answers2

2

Consider writing an Extension Method for whatever type your numbers are (int? long? float?). That way you could do:

int playerSalary = 20000000;
Print(playerSalary.ToLargeCurrencyFormat());
// Prints $20M

The extension method:

namespace ExtensionMethods
{
    public static class IntExtensions
    {
        public static string ToLargeCurrencyFormat(this int amount)
        {
            // Do your format conversion here
            return formattedString;
        }
    }   
}
jtheis
  • 916
  • 3
  • 12
  • 28
0

No. There is no parameter to the ToString method that can automatically format your currency values that way.

But you can easily code your own method to format the string that way.

Writing the if/else logic to get the display string is trivial and you can follow the sample code provided in the post you referenced in your question.

If you'd like to get fancy you may want to look into implementing a custom format provider.

See the MSDN documentation on IFormatProvider this post on SO.

Community
  • 1
  • 1
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151