Class Currency
java.lang.Object
java.util.Currency
A currency, identified by its ISO 4217 code: the subset of the JDK's
java.util.Currency a device needs to format and exchange amounts.
Currency euro = Currency.getInstance("EUR");
euro.getDefaultFractionDigits(); // 2
Currency.getInstance(Locale.JAPAN).getCurrencyCode(); // "JPY"
There is one instance per code, so two currencies compare with == as in
the JDK. The ISO 4217 codes, numeric codes and fraction digits, and the
currency each country uses, are compiled into this class; nothing is read
from the platform, so the answers are the same on every device and on the
server.
Differences from the JDK
getSymbol(Locale)knows two symbols per currency: the home one ($for US dollars in the US) when the locale's country uses the currency, and US English's (US$) otherwise. The JDK localizes further per language.getDisplayName(Locale)answers the English name for every locale. For a name in the user's language use the platform's localization (com.codename1.l10n.L10NManager).- The table is Java 17's plus the ISO 4217 changes since, fixed at
build time; there is no
java.util.currency.dataoverride, noCurrencyNameProviderand no serialization.
-
Method Summary
Modifier and TypeMethodDescriptionEvery currency this class knows, in a new set the caller may change.The ISO 4217 code, such as"EUR".intThe number of digits after the decimal point an amount normally has: 2 for the euro, 0 for the yen, 3 for the Bahraini dinar, and -1 for a pseudo currency such as gold (XAU).The English name, such as"Euro".getDisplayName(Locale locale) The English name for every locale; see the class description.static CurrencygetInstance(String currencyCode) The currency for an ISO 4217 code such as"EUR".static CurrencygetInstance(Locale locale) The currency the country oflocaleuses, or null for a country that uses none (Antarctica).intThe ISO 4217 numeric code, such as 978 for the euro.The numeric code as three digits, zero padded:"036"for the Australian dollar.The symbol in the default locale; seegetSymbol(Locale).The symbol inlocale: the home symbol ($for the US dollar in the US) when the locale's country uses this currency, else the symbol US English writes for it (CA$, andUS$for the dollar itself), which is the code for most currencies.toString()The ISO 4217 code.
-
Method Details
-
getInstance
-
getInstance
-
getAvailableCurrencies
-
getCurrencyCode
The ISO 4217 code, such as"EUR". -
getNumericCode
public int getNumericCode()The ISO 4217 numeric code, such as 978 for the euro. -
getNumericCodeAsString
The numeric code as three digits, zero padded:"036"for the Australian dollar. -
getDefaultFractionDigits
public int getDefaultFractionDigits()The number of digits after the decimal point an amount normally has: 2 for the euro, 0 for the yen, 3 for the Bahraini dinar, and -1 for a pseudo currency such as gold (XAU). -
getSymbol
The symbol in the default locale; seegetSymbol(Locale). -
getSymbol
The symbol in
locale: the home symbol ($for the US dollar in the US) when the locale's country uses this currency, else the symbol US English writes for it (CA$, andUS$for the dollar itself), which is the code for most currencies.Throws
NullPointerException: iflocaleis null
-
getDisplayName
The English name, such as"Euro". -
getDisplayName
-
toString
-