Can I get a text description of an ISO currency code in Java?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 03:06:45

You will have to set up your own mapping. Google or Stackoverflow questions will point to the ISO site

However you'll have to scrape the page as there seems to be no XML or text file as there is for country

Such a mapping would again be Locale-dependent… I think your best bet would be to take a long, hard look at ISO 4217 and create a Map from currency code to currency name.

It's not supported by Java but a few libraries can do this.

My number one choice would be ICU,

http://icu-project.org/apiref/icu4j/com/ibm/icu/util/Currency.html#getName%28java.util.Locale,%20int,%20boolean[]%29

This call can get you the name of a currency in multiple locales. ICU also supports all other i18n features not available in JRE. However, it's pretty big.

Another option is jPOS,

http://gl.jpos.org/

If you do anything with financial data, this is the de-facto standards. Watch for its license. Our lawyers didn't like it for some reason.

Class java.util.Currency implements this since java 1.7.

    Currency curr = Currency.getInstance("AZM");
    System.out.println(curr.getCurrencyCode()); // AZM
    System.out.println(curr.getNumericCode());  // 31
    System.out.println(curr.getDisplayName());  // Azerbaijani Manat (1993-2006)

Unfortunatelly, this class is still far from usability... e.g. missing constructor from numericCode, some displayNames containings not event DisplayName, ...

Not in the standard API. The data behind the Currency class is loaded from the package-private class java.util.CurrencyData, and there's simply no text description present there. You can look at it if you have the source code installed with your JDK.

openexchangerates.org provides this info live online in machine-readable JSON format:

http://openexchangerates.org/api/currencies.json

It returns information as simple as this:

{
  "AED": "United Arab Emirates Dirham",
  "AFN": "Afghan Afghani",

  ...

  "ZMK": "Zambian Kwacha",
  "ZWL": "Zimbabwean Dollar"
}

It's almost free but there are some terms and conditions. Here is the online documentation.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!