Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 508
DMac the Destroyer Avatar asked Jul 27 '12 21:07

DMac the Destroyer


People also ask

How do you format a negative currency?

The negative sign before the number but behind the currency symbol. The negative sign after the number. Enclosed in parentheses.

How do I show negative currency in Excel?

Select the cell or range of cells that you want to format with a negative number style. In the Category box, click either Number or Currency. Under Negative numbers, select an option for negative numbers.

Should negative numbers be in parentheses?

The standard accounting way is always to show negative numbers in parentheses. If you want to appeal to primarily financial professionals, that's the accepted practice. She also cautions against using red or drawing attention to a negative number.

Why is there parentheses in negative numbers?

Using a Number Line to Add Integers Look Out: sometimes you may see parentheses around negative numbers. These do not mean that we need to multiply; they're just used so that we don't confuse negatives with subtraction.


2 Answers

You have to 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

output:

-$1,234.43 
like image 189
Tim Schmelter Avatar answered Oct 08 '22 14:10

Tim Schmelter


I think a combination of the answers here will get 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 any code that deals with a single culture like this... I believe you need to build your own culture... Check this Question

like image 32
Sam Avatar answered Oct 08 '22 14:10

Sam