Raj Joshi
Raj Joshi

Reputation: 2669

How to get country name from currency code?

How to get country name from currency code? Is this possible or not?

Upvotes: 4

Views: 5605

Answers (3)

alioca
alioca

Reputation: 31

The country is not unique per currency. An alternative solution can be finding currency through country first (e.g. by using this), grouping by currency, and having a list of countries using that currency. Finally, you can select a representative country for those currencies with more than one country.

Assuming a pandas DataFrame with country and currency as its columns, grouping can be done with the following:

df.groupby('currency').country.apply(list).reset_index()     

Upvotes: 0

user28434'mstep
user28434'mstep

Reputation: 6600

Alternative option, using Swift 3 Locale class data:

struct CurrencyToRegionMapper {

    static let locales = Locale.availableIdentifiers.map(Locale.init)

    static func locales(currencyCode: String) -> Set<Locale> {
        let localesWithCode = self.locales.filter { locale in
            locale.currencyCode == currencyCode
        }
        return Set(localesWithCode)
    }

    static func locales(currencySymbol: String) -> Set<Locale> {
        let localesWithSymbol = self.locales.filter { locale in
            locale.currencySymbol == currencySymbol
        }
        return Set(localesWithSymbol)
    }

    static func regionNames(currencyCode: String, forLocale locale: Locale = Locale.autoupdatingCurrent) -> Set<String> {
        let locale = Locale(identifier: locale.identifier) // .current and .autoupdatingCurrent doesn't work without this hack for some reason
        let localesForCode = self.locales(currencyCode: currencyCode)
        let names: [String] = localesForCode.flatMap { loc in
            if let regionCode = loc.regionCode {
                return locale.localizedString(forRegionCode: regionCode)
            } else {
                return locale.localizedString(forIdentifier: loc.identifier)
            }
        }
        return Set(names)
    }

    static func regionNames(currencySymbol: String, forLocale locale: Locale = Locale.autoupdatingCurrent) -> Set<String> {
        let locale = Locale(identifier: locale.identifier) // .current and .autoupdatingCurrent doesn't work without this hack for some reason
        let localesForSymbol = self.locales(currencySymbol: currencySymbol)
        let names: [String] = localesForSymbol.flatMap { loc in
            if let regionCode = loc.regionCode {
                return locale.localizedString(forRegionCode: regionCode)
            } else {
                return locale.localizedString(forIdentifier: loc.identifier)
            }
        }
        return Set(names)
    }

}

Usage:

CurrencyToRegionMapper.locales(currencyCode: "EUR") // Returns set of locales, where currency is Euro
CurrencyToRegionMapper.locales(currencySymbol: "$") // Returns set of locales, where currency symbol is $

CurrencyToRegionMapper.regionNames(currencyCode: "GBP", forLocale: Locale(identifier: "en_POSIX")) // Returns set of regions(countries) names which use british pound in POSIX locale: ["United Kingdom", "Guernsey", "Jersey", "Isle of Man"]
CurrencyToRegionMapper.regionNames(currencySymbol: "¥", forLocale: Locale(identifier: "fi_FI")) // Returns set of regions which use currency with symbol ¥ in Finnish locale: ["Kiina", "Japani"] 

Upvotes: 7

kamwysoc
kamwysoc

Reputation: 6859

I found the whole list of currency codes and countires at this link

let codeToCountry = [
  "AED" : "United Arab Emirates",
  "AFN" : "Afghanistan",
  "ALL" : "Albania",
  "AMD" : "Armenia",
  "ANG" : "Netherlands Antilles",
  "AOA" : "Angola",
  "ARS" : "Argentina",
  "AUD" : "Australia, Australian Antarctic Territory, Christmas Island, Cocos (Keeling) Islands, Heard and McDonald Islands, Kiribati, Nauru, Norfolk Island, Tuvalu",
  "AWG" : "Aruba",
  "AZN" : "Azerbaijan",
  "BAM" : "Bosnia and Herzegovina",
  "BBD" : "Barbados",
  "BDT" : "Bangladesh",
  "BGN" : "Bulgaria",
  "BHD" : "Bahrain",
  "BIF" : "Burundi",
  "BMD" : "Bermuda",
  "BND" : "Brunei",
  "BOB" : "Bolivia",
  "BOV" : "Bolivia",
  "BRL" : "Brazil",
  "BSD" : "Bahamas",
  "BTN" : "Bhutan",
  "BWP" : "Botswana",
  "BYR" : "Belarus",
  "BZD" : "Belize",
  "CAD" : "Canada",
  "CDF" : "Democratic Republic of Congo",
  "CHE" : "Switzerland",
  "CHF" : "Switzerland, Liechtenstein",
  "CHW" : "Switzerland",
  "CLF" : "Chile",
  "CLP" : "Chile",
  "CNY" : "Mainland China",
  "COP" : "Colombia",
  "COU" : "Colombia",
  "CRC" : "Costa Rica",
  "CUP" : "Cuba",
  "CVE" : "Cape Verde",
  "CYP" : "Cyprus",
  "CZK" : "Czech Republic",
  "DJF" : "Djibouti",
  "DKK" : "Denmark, Faroe Islands, Greenland",
  "DOP" : "Dominican Republic",
  "DZD" : "Algeria",
  "EEK" : "Estonia",
  "EGP" : "Egypt",
  "ERN" : "Eritrea",
  "ETB" : "Ethiopia",
  "EUR" : "European Union, see eurozone",
  "FJD" : "Fiji",
  "FKP" : "Falkland Islands",
  "GBP" : "United Kingdom",
  "GEL" : "Georgia",
  "GHS" : "Ghana",
  "GIP" : "Gibraltar",
  "GMD" : "Gambia",
  "GNF" : "Guinea",
  "GTQ" : "Guatemala",
  "GYD" : "Guyana",
  "HKD" : "Hong Kong Special Administrative Region",
  "HNL" : "Honduras",
  "HRK" : "Croatia",
  "HTG" : "Haiti",
  "HUF" : "Hungary",
  "IDR" : "Indonesia",
  "ILS" : "Israel",
  "INR" : "Bhutan, India",
  "IQD" : "Iraq",
  "IRR" : "Iran",
  "ISK" : "Iceland",
  "JMD" : "Jamaica",
  "JOD" : "Jordan",
  "JPY" : "Japan",
  "KES" : "Kenya",
  "KGS" : "Kyrgyzstan",
  "KHR" : "Cambodia",
  "KMF" : "Comoros",
  "KPW" : "North Korea",
  "KRW" : "South Korea",
  "KWD" : "Kuwait",
  "KYD" : "Cayman Islands",
  "KZT" : "Kazakhstan",
  "LAK" : "Laos",
  "LBP" : "Lebanon",
  "LKR" : "Sri Lanka",
  "LRD" : "Liberia",
  "LSL" : "Lesotho",
  "LTL" : "Lithuania",
  "LVL" : "Latvia",
  "LYD" : "Libya",
  "MAD" : "Morocco, Western Sahara",
  "MDL" : "Moldova",
  "MGA" : "Madagascar",
  "MKD" : "Former Yugoslav Republic of Macedonia",
  "MMK" : "Myanmar",
  "MNT" : "Mongolia",
  "MOP" : "Macau Special Administrative Region",
  "MRO" : "Mauritania",
  "MTL" : "Malta",
  "MUR" : "Mauritius",
  "MVR" : "Maldives",
  "MWK" : "Malawi",
  "MXN" : "Mexico",
  "MXV" : "Mexico",
  "MYR" : "Malaysia",
  "MZN" : "Mozambique",
  "NAD" : "Namibia",
  "NGN" : "Nigeria",
  "NIO" : "Nicaragua",
  "NOK" : "Norway",
  "NPR" : "Nepal",
  "NZD" : "Cook Islands, New Zealand, Niue, Pitcairn, Tokelau",
  "OMR" : "Oman",
  "PAB" : "Panama",
  "PEN" : "Peru",
  "PGK" : "Papua New Guinea",
  "PHP" : "Philippines",
  "PKR" : "Pakistan",
  "PLN" : "Poland",
  "PYG" : "Paraguay",
  "QAR" : "Qatar",
  "RON" : "Romania",
  "RSD" : "Serbia",
  "RUB" : "Russia, Abkhazia, South Ossetia",
  "RWF" : "Rwanda",
  "SAR" : "Saudi Arabia",
  "SBD" : "Solomon Islands",
  "SCR" : "Seychelles",
  "SDG" : "Sudan",
  "SEK" : "Sweden",
  "SGD" : "Singapore",
  "SHP" : "Saint Helena",
  "SKK" : "Slovakia",
  "SLL" : "Sierra Leone",
  "SOS" : "Somalia",
  "SRD" : "Suriname",
  "STD" : "São Tomé and Príncipe",
  "SYP" : "Syria",
  "SZL" : "Swaziland",
  "THB" : "Thailand",
  "TJS" : "Tajikistan",
  "TMM" : "Turkmenistan",
  "TND" : "Tunisia",
  "TOP" : "Tonga",
  "TRY" : "Turkey",
  "TTD" : "Trinidad and Tobago",
  "TWD" : "Taiwan and other islands that are under the effective control of the Republic of China (ROC)",
  "TZS" : "Tanzania",
  "UAH" : "Ukraine",
  "UGX" : "Uganda",
  "USD" : "American Samoa, British Indian Ocean Territory, Ecuador, El Salvador, Guam, Haiti, Marshall Islands, Micronesia, Northern Mariana Islands, Palau, Panama, Puerto Rico, East Timor, Turks and Caicos Islands, United States, Virgin Islands",
  "USN" : "United States",
  "USS" : "United States",
  "UYU" : "Uruguay",
  "UZS" : "Uzbekistan",
  "VEB" : "Venezuela",
  "VND" : "Vietnam",
  "VUV" : "Vanuatu",
  "WST" : "Samoa",
  "XAF" : "Cameroon, Central African Republic, Congo, Chad, Equatorial Guinea, Gabon",
  "XAG" : "",
  "XAU" : "",
  "XBA" : "",
  "XBB" : "",
  "XBC" : "",
  "XBD" : "",
  "XCD" : "Anguilla, Antigua and Barbuda, Dominica, Grenada, Montserrat, Saint Kitts and Nevis, Saint Lucia, Saint Vincent and the Grenadines",
  "XDR" : "International Monetary Fund",
  "XFO" : "Bank for International Settlements",
  "XFU" : "International Union of Railways",
  "XOF" : "Benin, Burkina Faso, Côte d'Ivoire, Guinea-Bissau, Mali, Niger, Senegal, Togo",
  "XPD" : "",
  "XPF" : "French Polynesia, New Caledonia, Wallis and Futuna",
  "XPT" : "",
  "XTS" : "",
  "XXX" : "",
  "YER" : "Yemen",
  "ZAR" : "South Africa",
  "ZMK" : "Zambia",
  "ZWD" : "Zimbabwe"]

and some function to parse dict:

func countryBy(code : String) -> String? {
    return codeToCountry[code]
}

countryBy(code: "ZWD")
countryBy(code: "")
countryBy(code: "asda")

Here it's some working example:

https://iswift.org/playground?blvcE5

Upvotes: 6

Related Questions