1

I have a Xamarin.Forms app that is obtaining a 2-letter ISO country code for GeoLocation. I need the app to display currency in the local style. So if I took my phone from the UK to Japan it would display currency fields formatted up in Yen.

The most straight forward way to format a decimal currency is to use a CultureInfo object.

How can I get to a CultureInfo object from a 2-letter ISO country code. I have been trying to achieve this for quite a few days, but cannot find a path of conversions.

Any pointers?

JDibble
  • 744
  • 1
  • 8
  • 25
  • What is the problem with _CultureInfo ci = new CultureInfo("ja");_? – Steve Aug 25 '17 at 15:18
  • [National Language Support (NLS) API Reference](https://msdn.microsoft.com/en-us/library/cc233982.aspx) – fshauge Aug 25 '17 at 15:21
  • @Steve If I run the code here in the UK I get an ISO code "GB". CutureInfo ci = new CultureInfo("GB"); throws a CultureNotFoundException as it expects "en-GB" for the UK – JDibble Aug 25 '17 at 15:27
  • 4
    Possible duplicate of [How to map a two letter ISO-3166 country code to a Culture in C#](https://stackoverflow.com/questions/34765235/how-to-map-a-two-letter-iso-3166-country-code-to-a-culture-in-c-sharp) –  Aug 25 '17 at 15:33
  • "So if I took my phone from the UK to Japan it would display currency fields formatted up in Yen." Yes, except £10 isn't ¥10 (at the time of writing), so be careful what you wish for. Cultures don't do exchange rates. If the user has entered a number with no currency, you might use the culture to infer it, but beyond that, you want to store the actual currency as soon as possible. – Jeroen Mostert Aug 25 '17 at 15:49
  • Hi Jeroen, I wasn't looking for a currency conversion, just looking to format a currency entry correctly for the country that the device is being used in allowing for the fact that the device would be setup for the user's "home" country – JDibble Aug 28 '17 at 19:56

2 Answers2

8
var isoCountryCode = "GB"; // Replace with the one you got from GeoLocation.
var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures)
                          .Where(c => c.Name.EndsWith("-" + isoCountryCode )).First;

If you check the list of CultureCodes provided by microsoft you can see that the first two letters of the culture refer to language and the last two to country. For example, for japanese you would have "ja" and "ja-JP").

Therefore if you search through the list of cultures for the ones that end with your desired country code, you should get a list of all available CultureInfo for your desired country.

Then you can either go through them again to find the one for the users selected language (if available) or pick one arbitrarily.

  • 1
    Be aware with using the above code. The returned culture info can be incorrect. In the above example it returns cy-GB, where en-GB would be the prefered result. – oceanmountain May 17 '18 at 12:06
-1

You can just search all cultures by 2-letter code:

CultureInfo.GetCultures(CultureTypes.AllCultures)
           .Where(cu => cu.TwoLetterISOLanguageName == code);

Just note that there may be multiple results.

icebat
  • 4,696
  • 4
  • 22
  • 36
  • 1
    The code is a country not a language, so this will not solve the issue – JDibble Aug 25 '17 at 17:40
  • The task was to get the CultureInfo based on two letter ISO **Country Code** the code above gets the CultureInfo based on ISO **Language** name. Country Code and Language code is not the same. – 1iveowl Mar 22 '19 at 07:08