Acquiring a country's currency code

后端 未结 4 652
逝去的感伤
逝去的感伤 2020-12-19 11:45

I have a problem getting a country\'s currency code. My task is to get the user\'s location, find out what country he is right now and get this country\'s currency code. Her

相关标签:
4条回答
  • 2020-12-19 11:55

    Keep in mind that the result of doing this will be inaccurate since we don't have the Language Code, or it can even throw an exception due to not having that Locale on the system. Instead I'd search for it doing the following:

    val locale = Locale.getAvailableLocales().first { it.country == address.countryCode }
    val currency = Currency.getInstance(locale)
    

    Also, if you want to try to be as accurate as possible, you could do this:

    val locale =
        Locale.getAvailableLocales().firstOrNull { it.country.equals(countryCode, true) && it.language.equals(countryCode, true) }
            ?: Locale.getAvailableLocales().firstOrNull { it.country.equals(countryCode, true) }
            ?: Locale.getDefault()
    val currency = Currency.getInstance(locale)
    
    0 讨论(0)
  • 2020-12-19 11:57

    Why not you use Address object to get Locale, by method:

    http://developer.android.com/reference/android/location/Address.html#getLocale%28%29

    0 讨论(0)
  • 2020-12-19 12:01

    You should be able to use Currency.getInstance(new Locale("",code)), with a possible Exception if the country code isn't valid.

    0 讨论(0)
  • 2020-12-19 12:13
    String lang = Locale.getDefault().getDisplayLanguage();
    Locale locale = new Locale(lang, COUNTRY_YOU_HAVE);
    
    0 讨论(0)
提交回复
热议问题