Can I get a standard currency format for using a negative sign instead of parentheses?

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

My web.config has a culture set to auto and it allows en-US .

The ideal solution would be some kind of global web.config or other parameter that would force {0:C} display a negative sign for the en-US culture, but I am open to other, less frightening solutions.

+7
source share
3 answers

I think the combination of answers here will bring you closer to what you want.

 protected void Application_BeginRequest() { var ci = CultureInfo.GetCultureInfo("en-US"); if (Thread.CurrentThread.CurrentCulture.DisplayName == ci.DisplayName) { ci = CultureInfo.CreateSpecificCulture("en-US"); ci.NumberFormat.CurrencyNegativePattern = 1; Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; } } 

If you want to not have code that deals with a culture like this ... I believe you need to create your own culture ... Check this question

+10
source

You should specify the correct NumberFormatInfo.CurrencyNegativePattern , which is probably 1.

 Decimal dec = new Decimal(-1234.4321); CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US"); culture.NumberFormat.CurrencyNegativePattern = 1; String str = String.Format(culture, "{0:C}", dec); Console.Write(str); 

demo: http://ideone.com/HxSqT

exit:

 -$1,234.43 
+9
source

As far as I understand your question.

You want to display the currency format depending on the culture.

Every time you do some cultural things, .NET looks at Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture .

You can set the desired culture in ASP.NET in the global.asax BeginRequest method.

 protected void Application_BeginRequest() { var ci = CultureInfo.GetCultureInfo("en-US"); // put the culture you want in here Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; } 
+1
source

All Articles