Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Currency to ALWAYS use USD regardless of users culture

This should be simple but I cannot find the answer anywhere. On our asp.net MVC site, we show currency values and I need these currency values to Always be in USD regardless of the users culture settings.

If I use string.format("{0:C}",value) then it shows the value in what ever culture the use has set. This is incorrect for us as $1000.00 is not the same thing as 1000.00 euro

I still need to use the language side of this, meaning if they are in France I want to use the French localization resource, so I don't want to completely disregard their culture settings, but how can I make sure the currency is always shown in USD?

like image 668
twaldron Avatar asked Nov 05 '12 16:11

twaldron


2 Answers

Explicitly pass culture to the Format call:

string.Format(new CultureInfo("en-US"), "{0:C}",value) 

If you have to mix it with other languages - save result of formatting currency value and insert into other places as string.

Side note: using "en-US" (or any other hard-coded culture) will lead to potentially mismatched representation of negative values i.e. some cultures use -100, while other (100) for negative amounts.

like image 63
Alexei Levenkov Avatar answered Sep 28 '22 06:09

Alexei Levenkov


You would have to specify CurrentCulture to be en-US and leave CurrentUICulture as is.

I usually do this in global.asax.cs in the Application_BeginRequest event

System.Threading.Thread.CurrentThread.CurrentCulture = 
     new System.Globalization.CultureInfo("en-US", false);

It is possible to specify the culture in web.config file but that has one drawback - it takes into account the customized regional settings of the server (if the administrator changed the currency or date format on the server for the en-US culture, it would get picked up this way) - so you can't ever be sure that the application will behave exactly the same as when you created/tested it.

like image 24
Knaģis Avatar answered Sep 28 '22 08:09

Knaģis