开发者

ASP.NET Change Culture for ONLY Currency not date, etc

开发者 https://www.devze.com 2023-02-19 16:44 出处:网络
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

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 effe开发者_高级运维ct the currency and nothing else, all dates, etc should be in the default culture(en-US)

Any ideas?


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


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!


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.


using System.Globalization;

...

value.ToString(CultureInfo.CreateSpecificCulture("en-US"));


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消