1

I have a base class that inherits a page and changes the culture based on a pre-determined value set in the database. I need the culture to change the currency symbol but nothing else. If the value in the db says en-GB I need for it to change all currency values on the page to British pounds, and if it sais en-US show a US Dollar Sign. I need the culture variable to only effect the currency and nothing else, all dates, etc should be in the default culture(en-US)

Any ideas?

EvanGWatkins
  • 1,427
  • 6
  • 23
  • 52

5 Answers5

2

Basically you need to use a format provider when formatting your numbers as currency. Have a look at the following example:

public static string CulturedCurrency(decimal number,string culture = "en-US")
{
       NumberFormatInfo numberInfo = CultureInfo.CreateSpecificCulture(culture).NumberFormat;
        return number.ToString("c",numberInfo);
} 

Reference: http://geekswithblogs.net/dtotzke/articles/24573.aspx

If you want to do it inline on databinding have a look at the code here: Format string by CultureInfo

Community
  • 1
  • 1
Rob Stevenson-Leggett
  • 35,279
  • 21
  • 87
  • 141
2

I have found the solution I was looking for. Going through and changing each element of currency to use the specified culture was not something that would be easily done so I started playing with other options and what I have found was that if I used the culture function in my base class I could do the following:

 System.Globalization.CultureInfo ci;
    if (culture == "")
    {
        ci = new System.Globalization.CultureInfo("en-US");

    }
    else
    {
        ci = new System.Globalization.CultureInfo(culture);
    }

    System.Threading.Thread.CurrentThread.CurrentCulture = ci;
    System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
    ci.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
    ci.DateTimeFormat.LongDatePattern = "dddd, MMMM dd, yyyy";
    ci.DateTimeFormat.DateSeparator = "/";

This will set the culture to what I want and then set the date of the culture (no matter what the culture is) to the US Format of the datetime. Thanks for all the help!

EvanGWatkins
  • 1,427
  • 6
  • 23
  • 52
  • The .LongDatePattern will set the proper order of the day-of-week, month, day and year but they will be set in the native language. e.g. en-US: "Tuesday, April 06, 2010" will become in it-IT: "martedì, aprile 06, 2010". Right order, wrong language. – Mark A Apr 30 '11 at 01:02
0

A little add on Rob's answer, if you have a localization attribute in your URL, and thus setting the Culture on every request, you might just want to do it like this:

//get language attribute from url. (requires special routing in MVC)
string lang = (string)filterContext.RouteData.Values["lang"] ?? _DefaultLanguage;
switch (lang)
{
    case "nl":
        lang = "nl-NL";
        break;
    case "en":
        lang = "en-GB";
        break;
    case "en-US":
        lang = "en-US";
        break;
    default:
        lang = _DefaultLanguage;//nl-NL
        break;
}

NumberFormatInfo numberInfo = CultureInfo.CreateSpecificCulture("nl-NL").NumberFormat; //always use euros as currency
CultureInfo info = new CultureInfo(lang);
info.NumberFormat = numberInfo;
CularBytes
  • 9,924
  • 8
  • 76
  • 101
0

Most ToString methods take a format provider; the Culture Info is a format provider. You are going to have to leave the current culture as en-US and manually format the currency values using the ToString() method.

http://msdn.microsoft.com/en-us/library/3ebe5aks.aspx

HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0
using System.Globalization;

...

value.ToString(CultureInfo.CreateSpecificCulture("en-US"));
Adrian Toman
  • 11,316
  • 5
  • 48
  • 62