如何从货币代码获取国家代码/名称

4

如果我有一个货币代码,如何获取适用该货币的国家代码/名称?我知道货币和国家之间可能存在一对多的映射关系,那么如何获取应用该货币的所有国家?


1
你可以将货币/国家组合存储在数据库表中。当金融世界的情况发生变化时,您需要更新该表。 - Gregor Raýman
不想自己维护映射关系。这里没有相关的API吗? - akrohit
尝试做同样的事情。有货币代码。想要将其与国旗图标匹配。 - filthy_wizard
2个回答

1
nv-i18n库中的CurrencyCode类有一个getCountryList()方法,返回使用该货币的国家列表。以下是一个示例命令行应用程序。
import java.util.List;
import com.neovisionaries.i18n.CountryCode;
import com.neovisionaries.i18n.CurrencyCode;

public class CurrencyToCountryList
{
    public static void main(String[] args)
    {
        // For each currency code such as EUR and USD.
        for (String code : args)
        {
            // Get a CurrencyCode instance.
            CurrencyCode currency =
                CurrencyCode.getByCode(code);

            if (currency == null)
            {
                // The code is invalid.
                System.out.format("'%s' is invalid.\n", code);
                continue;
            }

            System.out.format("%s (%s) is used by:\n",
                currency, currency.getName());

            // Get the list of countries using the currency.
            List<CountryCode> countryCodeList =
                currency.getCountryList();

            // For each country.
            for (CountryCode country : countryCodeList)
            {
                // Print the country code and the country name.
                System.out.format("\t%s: %s\n",
                    country, country.getName());
            }
        }
    }
}

如果您以如下方式运行此应用程序:
$ java -cp nv-i18n-1.15.jar:. CurrencyToCountryList USD

您将获得以下显示结果:
USD (US Dollar) is used by:
  AS: American Samoa
  BQ: Bonaire, Sint Eustatius and Saba
  EC: Ecuador
  FM: Micronesia, Federated States of
  GU: Guam
  HT: Haiti
  IO: British Indian Ocean Territory
  MH: Marshall Islands
  MP: Northern Mariana Islands
  PA: Panama
  PR: Puerto Rico
  PW: Palau
  SV: El Salvador
  TC: Turks and Caicos Islands
  TL: Timor-Leste
  UM: United States Minor Outlying Islands
  US: United States
  VG: Virgin Islands, British
  VI: Virgin Islands, U.S.

0
为了获取某种货币的所有国家,您可以在Java中运行一个简单的搜索MultiMap对象。通过在地图中运行for循环进行搜索(第一个字符串是货币,第二个字符串是国家),并使用if检查是否为您要查找的货币。如果是真的,您可以将所有找到的国家添加到数组中,然后进一步使用它们。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接