Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class Currency
- Object
- 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.
Methods
public static Currency getInstance(String currencyCode) | The currency for an ISO 4217 code such as "EUR". |
public static Currency getInstance(Locale locale) | The currency the country of locale uses, or null for a country that uses none (Antarctica). |
public static Set<Currency> getAvailableCurrencies() | Every currency this class knows, in a new set the caller may change. |
public String getCurrencyCode() | The ISO 4217 code, such as "EUR". |
public int getNumericCode() | The ISO 4217 numeric code, such as 978 for the euro. |
public String getNumericCodeAsString() | The numeric code as three digits, zero padded: "036" for the Australian dollar. |
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). |
public String getSymbol() | The symbol in the default locale; see getSymbol(Locale). |
public String getSymbol(Locale locale) | 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$, and US$ for the dollar itself), which is the code for most currencies. |
public String getDisplayName() | The English name, such as "Euro". |
public String getDisplayName(Locale locale) | The English name for every locale; see the class description. |
public String toString() | The ISO 4217 code. |
Inherited methods
Method details
getInstance
public static Currency getInstance(String currencyCode)The currency for an ISO 4217 code such as
"EUR".Throws
NullPointerException- if
currencyCodeis null IllegalArgumentException- if it is not a known ISO 4217 code
getInstance
public static Currency getInstance(Locale locale)The currency the country of
locale uses, or null for a country
that uses none (Antarctica).Throws
NullPointerException- if
localeis null IllegalArgumentException- if the locale has no country, or one that is not two ASCII letters
getAvailableCurrencies
public static Set<Currency> getAvailableCurrencies()Every currency this class knows, in a new set the caller may change.
getCurrencyCode
public String getCurrencyCode()The ISO 4217 code, such as
"EUR".getNumericCode
public int getNumericCode()The ISO 4217 numeric code, such as 978 for the euro.
getNumericCodeAsString
public String 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
public String getSymbol()The symbol in the default locale; see
getSymbol(Locale).getSymbol
public String getSymbol(Locale locale)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$, and US$ for the dollar itself), which
is the code for most currencies.Throws
NullPointerException- if
localeis null
getDisplayName
public String getDisplayName()The English name, such as
"Euro".getDisplayName
public String getDisplayName(Locale locale)The English name for every locale; see the class description.
Throws
NullPointerException- if
localeis null
toString
public String toString()The ISO 4217 code.