Get the currency from current culture?
Asked Answered
M

8

58

Is there a way to get current information dynamically from the apps culture settings? Basically if the user has set the culture to US I want to know the currency is dollars, or if they have it set to UK I want to pound sterling etc... etc..

This is so I can send this information to PayPal when a payment is being made

Mcneal answered 4/5, 2010 at 6:1 Comment(1)
I think paypal expects "USD" which is ISO Currency Code.Fidelis
C
98

Use the RegionInfo.ISOCurrencySymbol property. For example:

  var ri = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
  Console.WriteLine(ri.ISOCurrencySymbol);

Output: "USD"

Cidevant answered 4/5, 2010 at 7:22 Comment(1)
This should work perfectly! I'll try it this afternoon and mark as the answer if it works :) ThanksMcneal
L
44

You can get the symbol from CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol, but I doubt this is enough; you may need to maintain a separate list per culture. Or just let the user tell you what they want to pay in (for example, they might be away from home, etc, so the culture of the PC in some hotel lounge isn't what is on their credit card)

Luciennelucier answered 4/5, 2010 at 6:15 Comment(1)
Exactly what I was looking for but you have to import System.Globalization first. ThanksSuperable
E
27

Once you have the CultureInfo ci object, you can ask such as

ci.NumberFormat.CurrencySymbol

For current culture, you will simply do

CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol
Elliotelliott answered 4/5, 2010 at 6:7 Comment(2)
Not necessariy good enough as the currency SYMBOL may not be unique.Sweven
This will return not ISO currency symbols. For example instead of EUR you will get euro symbol like € .Cinerarium
E
6
string isoCurrencySymbol = RegionInfo.CurrentRegion.ISOCurrencySymbol;
Earwax answered 27/10, 2011 at 10:8 Comment(3)
The ISOCurrencySymbol is the 3-letter code (e.g., USD). The symbol or sign (e.g., $) as retrieved by NumberFormat.CurrencySymbol, is NOT unique.Bridgers
Just a warning that CurrentRegion does NOT come from the current UI culture, and changing the culture of the current thread will not change CurrentRegion - learn.microsoft.com/en-us/dotnet/api/… To do that see the answer by @Hans PassantOutspread
@Outspread for most app the OS setting is the one to use. So no issue.Mountbatten
N
2

You can basically use CultureInfo class

CultureInfo ci = new CultureInfo(UICulture);
var symbol = ci.NumberFormat.CurrencySymbol;
Norahnorbert answered 4/5, 2010 at 6:7 Comment(1)
Not good enough - sorry. THe currency symbol is not the currency - there is no requirement for that being unique.Sweven
S
1
    public static string GetCurrencySymbol(string currency)
    {
        if (currency == null) return "";
        if (currency == "") return "";
        int i = 0;
        var regionInfo = new RegionInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
        foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
        {
            if (!cultureInfo.Equals(CultureInfo.InvariantCulture))
            {
                var regionCulture = new RegionInfo(cultureInfo.LCID);

                    if(regionCulture.ISOCurrencySymbol == currency)
                    {
                        //list.Add(regionCulture);
                        regionInfo = regionCulture;
                    }
                }
        }
Stridulate answered 13/12, 2010 at 7:55 Comment(1)
O(N) search each time?? Why not make it a recursive function and do some memoization?Adlib
P
1

this worked for me.

var c = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
      .Select(t=> new RegionInfo(t.LCID))
      .Where(t=>t.ThreeLetterISORegionName  == "USA")
      .FirstOrDefault();
Permeance answered 11/5, 2020 at 11:20 Comment(0)
R
0

http://help.outlook.com/en-us/140/system.globalization.regioninfo.currencynativename(VS.85).aspx

You'll want the RegionInfo.CurrencyNativeName, RegionInfo.CurrencyEnglishName or RegionInfo.ISOCurrencySymbol

Responsible answered 2/6, 2010 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.