Can I get the standard currency format to use a negative sign instead of parentheses?

Viewed 13607

There are many places in my project where I try to display currency with the built-in {0:C} currency format. If the number is negative, it surrounds the value in parentheses. I want it to use a negative sign instead.

My web.config has culture set to auto, and it resolves to en-US.

The ideal solution would be some global web.config or other setting that would make the {0:C} display the negative sign for the en-US culture, but I'm open to other, less awesome solutions as well.

4 Answers

A solution with the following advantages over the other answers:

  • Clones the system's other existing CurrentCulture settings instead of resetting them to en-US defaults.
  • Only modifies CurrentCulture which deals with currency, not CurrentUICulture where the system may have a different text language set.
  • Only modifies CurrencyNegativePattern if the system does not specify a custom setting for it.
  • Minimizes extra calls.
if (Thread.CurrentThread.CurrentCulture.Name.Equals("en-US", StringComparison.Ordinal)
    && Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyNegativePattern == 0)
{
    CultureInfo cultureInfo = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
    cultureInfo.NumberFormat.CurrencyNegativePattern = 1;
    Thread.CurrentThread.CurrentCulture = cultureInfo;
}

Alternate solution

If you want to change the CurrencyNegativePattern but don't want to check if the system is set to en-US or if the system has an existing custom CurrencyNegativePattern setting, but keeping the other advantages of the first solution:

CultureInfo cultureInfo = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
cultureInfo.NumberFormat.CurrencyNegativePattern = 1;
Thread.CurrentThread.CurrentCulture = cultureInfo;
Related