Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally changing format of negative currency numbers in C#

Tags:

c#

asp.net

c#-4.0

We have a large ASP.NET MVC project where all numbers output to the screen are formatted as currency (i.e. ToString("c"). However, negative numbers are showing up with ()'s. For example:

decimal d = -8.88m;
Console.WriteLine(d.ToString("c"));
//outputs $(8.88)

This is a bit annoying to our users, particularly since there are in textboxes. We have a few thousand places where we send currency fields to the screen like this, so we'd love a way to change the formatting globally. Is there one? All the methods I've seen indicate that you have to create a new formatter, similar to this:

 string curCulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
 System.Globalization.NumberFormatInfo currencyFormat =
     new System.Globalization.CultureInfo(curCulture).NumberFormat;
 currencyFormat.CurrencyNegativePattern = 1;

We'd prefer not to change all of our ToString("c") methods ... is there a better way? My first thought was to just change our locale to Australia, but realized the date formatting would be screwed up.

like image 953
Beep beep Avatar asked Oct 22 '10 07:10

Beep beep


1 Answers

Aliostad was close ... try this in your base controller:

        System.Globalization.CultureInfo modCulture = new System.Globalization.CultureInfo("en-US");
        modCulture.NumberFormat.CurrencyNegativePattern = 1;
        Thread.CurrentThread.CurrentCulture = modCulture;
like image 89
user268137 Avatar answered Sep 17 '22 15:09

user268137