Home  >  Core Java

Java Currency Example

By Arvind Rai, 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.



getInstance

The getInstance method returns the Currency instance.
static Currency getInstance(Locale locale)
static Currency getInstance(String currencyCode) 
We can pass either 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

The getDisplayName returns the suitable name for this currency.
String getDisplayName()
String getDisplayName(Locale locale) 
The display name for the currency can be obtained for default locale or specified locale. When we don’t pass locale, default locale is considered. In both cases, if there is no suitable display name found, the ISO 4217 currency code is returned.
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

The getNumericCode 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() 
Example:
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

The getSymbol gets the symbol of this currency.
String getSymbol()
String getSymbol(Locale locale) 
When locale is not passed to 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

The getCurrencyCode returns the ISO 4217 currency code of this currency.
String getCurrencyCode() 
Example:
Currency c = Currency.getInstance(Locale.CANADA);
String code = c.getCurrencyCode();
System.out.println(code); // CAD 

getAvailableCurrencies

The getAvailableCurrencies is a static method that returns set of available currencies.
static Set<Currency> getAvailableCurrencies() 
Example:
Set<Currency> set = Currency.getAvailableCurrencies();
set.forEach(c -> System.out.println(c)); 

getDefaultFractionDigits

The getDefaultFractionDigits 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() 
Example:
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, use java.text.NumberFormat. To instantiate NumberFormat for currency, it has following static methods.
static final NumberFormat getCurrencyInstance()
static NumberFormat getCurrencyInstance(Locale inLocale) 
When we don’t pass locale, the default locale is considered.
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 

Reference

Class Currency
POSTED BY
ARVIND RAI
ARVIND RAI











©2021 concretepage.com | Privacy Policy | Contact Us