user1947702
user1947702

Reputation: 154

C# display money format like Sensible Soccer

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

Upvotes: 0

Views: 157

Answers (2)

jtheis
jtheis

Reputation: 916

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;
        }
    }   
}

Upvotes: 2

Mike Dinescu
Mike Dinescu

Reputation: 55760

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.

Upvotes: 0

Related Questions