Currency in Java
September 08, 2021
On this page we will learn Java java.util.Currency
API.
1. Java
Currency
class represents a currency. This is a final
class.
2. To instantiate currency, use its
getInstance
method. The Currency
class has no public constructor because it is designed so that there is never more than one Currency
instance for any given currency.
3. Java recommends to use
BigDecimal
to deal with Currency
or monetary values as it provides better handling of floating point numbers and their operations.
4. The currency is formatted using
NumberFormat
for different locale. Instantiate NumberFormat
using its getCurrencyInstance
method.
Now let us learn using
Currency
API. Here we will provide code to use methods of Currency
API and will also learn to format currency according to different locale.
Contents
getInstance
ThegetInstance
method returns the Currency
instance.
static Currency getInstance(Locale locale) static Currency getInstance(String currencyCode)
Locale
or currency code. These methods are static and can be called directly by class.
Example:
Currency c1 = Currency.getInstance(Locale.FRANCE); System.out.println(c1); // EUR Currency c2 = Currency.getInstance("JPY"); System.out.println(c2); // JPY
getDisplayName
ThegetDisplayName
returns the suitable name for this currency.
String getDisplayName() String getDisplayName(Locale locale)
Example:
Currency c = Currency.getInstance(Locale.FRANCE); String displayName1 = c.getDisplayName(); // Default Locale System.out.println(displayName1); // Euro String displayName2 = c.getDisplayName(Locale.US); System.out.println(displayName2); // Euro
getNumericCode and getNumericCodeAsString
ThegetNumericCode
returns the ISO 4217 numeric code of this currency.
The
getNumericCodeAsString
returns the 3 digit ISO 4217 numeric code of this currency as string. The value returned by getNumericCodeAsString
will always be of 3 digit. For numeric code 32, it will return 032 and for numeric code 6, it will return 006.
int getNumericCode() String getNumericCodeAsString()
Currency c = Currency.getInstance("AUD"); // Australian Dollar int code = c.getNumericCode(); System.out.println(code); // 36 String codeStr = c.getNumericCodeAsString(); System.out.println(codeStr); // 036
getSymbol
ThegetSymbol
gets the symbol of this currency.
String getSymbol() String getSymbol(Locale locale)
getSymbol
, it returns the symbol of currency for default locale.
Example:
Currency c = Currency.getInstance("AUD"); // Australian Dollar String s1 = c.getSymbol(); System.out.println(s1); // A$ String s2 = c.getSymbol(Locale.CHINA); System.out.println(s2); // AU$
getCurrencyCode
ThegetCurrencyCode
returns the ISO 4217 currency code of this currency.
String getCurrencyCode()
Currency c = Currency.getInstance(Locale.CANADA); String code = c.getCurrencyCode(); System.out.println(code); // CAD
getAvailableCurrencies
ThegetAvailableCurrencies
is a static method that returns set of available currencies.
static Set<Currency> getAvailableCurrencies()
Set<Currency> set = Currency.getAvailableCurrencies(); set.forEach(c -> System.out.println(c));
getDefaultFractionDigits
ThegetDefaultFractionDigits
returns the default number of fraction digits used with this currency. Number of fraction digits is same as 4217's minor unit for the currency.
int getDefaultFractionDigits()
Currency c1 = Currency.getInstance(Locale.GERMANY); int fdig = c1.getDefaultFractionDigits(); System.out.println(fdig); // 2
Formatting the Currency
To format the currency according to different locale, usejava.text.NumberFormat
. To instantiate NumberFormat
for currency, it has following static methods.
static final NumberFormat getCurrencyInstance() static NumberFormat getCurrencyInstance(Locale inLocale)
Find the code snippet to format a currency.
BigDecimal b = new BigDecimal(123.50); NumberFormat formatter1 = NumberFormat.getCurrencyInstance(Locale.UK); String c1 = formatter1.format(b); System.out.println(c1); // £123.50 NumberFormat formatter2 = NumberFormat.getCurrencyInstance(Locale.US); String c2 = formatter2.format(b); System.out.println(c2); // $123.50