December 27, 2011 12:00pm
Negative Currency Pattern

This might be old news to some but I learned about this today.

On a decimal type which is your dollar amount you typically call theAmount.ToString(ā€œCā€) which outputs $1.00, however if you have a negative value this same .ToString(ā€œCā€) will output ($1.00)

This actually is the default behavior of .NET when handling negative dollars. If you want to alter this you can change the NumberFormatInfo.CurrencyNegativePattern Property.

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

var newCulture = new CultureInfo(currentCulture.IetfLanguageTag)

                                         {

                                             NumberFormat =

                                                 {

                                                     CurrencyNegativePattern = 1 //pattern of -$n

                                                 }

                                         };

Thread.CurrentThread.CurrentCulture = newCulture;

Now your .ToString() calls can remain the same but you get your desired display.

Blog comments powered by Disqus