2015-04-06 115 views
1

獲取國家代碼/名稱鑑於我有貨幣代碼,我如何獲取貨幣適用的國家代碼/名稱?我知道貨幣和國家之間可能會有1:n的映射關係,那麼該如何獲得貨幣適用的所有國家?如何從貨幣代碼

+1

您可以在貨幣/國家組合存儲在數據庫中的表。當財務狀況發生變化時,您需要更新表格。 – 2015-04-06 11:08:08

+0

不想自己維護映射。這不是一些API嗎? – akrohit 2015-04-06 11:09:19

回答

0

爲了獲得貨幣的所有國家,您可以運行一個簡單的搜索到Java中的MultiMap對象。通過在地圖上運行搜索(例如,第一個字符串是貨幣,第二個是國家)以及如果檢查貨幣是否是您要查找的貨幣。如果是這樣,您可以將所有找到的國家添加到數組中,然後進一步使用它們。

0

CurrencyCodenv-i18n庫有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.