java.util.Currency methods with example
This class represents currency. Here, currency is identified by their ISO 4217 currency codes. The purpose of ISO 4217 is to establish internationally recognized codes for the representation of currencies. Currencies can be represented in the code in two ways: a three-letter alphabetic code and a three-digit numeric code.
util.Currency methods in Java
/ / | \ \
getCurrency getInstance getDisplayName getSymbol getDefaultFractionDigits
Some of the Currency class methods :
- getCurrency() : java.util.Currency.getCurrency() method returns ISO 4217 currency code of the passed currency argument.
Syntax :public String getCurrencyCode() Return : ISO 4217 currency code of the passed argument.
- getInstance() : java.util.Currency.getInstance() method creates currency instance for Currency code.
Syntax :public static Currency getInstance(String cCode) Parameter : cCode - ISO 4217 currency code of the passed currency argument Return : currency instance for Currency code
- getDefaultFractionDigits() : java.util.Currency.getDefaultFractionDigits() method returns default number of argumented currency fraction digits.
Syntax :public int getDefaultFractionDigits() Return : returns default number of argumented currency fraction digits.
- getDisplayName() : java.util.Currency.getDisplayName() method generates the currency name of the argumented currency code.
Syntax :public string getDisplayName() Return : currency name of the argumented currency code
- getSymbol() : java.util.Currency.getSymbol() method returns Currency symbol for the argumented currency code. In case, no symbol is returned normal currency code will be returned.
Syntax :public String getSymbol() Return : Symbol of the argumented currency code currency code.
Java code explaining getInstance(), getCurrencyCode(),getDefaultFractionDigits(), getDisplayName(), getSymbol() methods in Currency class
// Java program explaining Currency class methods// getInstance(), getCurrencyCode(),getDefaultFractionDigits()// getDisplayName(), getSymbol()importjava.util.*;publicclassNewClass{publicstaticvoidmain(String[] args){// Use of getInstance() method to 'AUD' instanceCurrency c1 = Currency.getInstance("AUD");//Australian DollarCurrency c2 = Currency.getInstance("JPY");//Japan YenCurrency c3 = Currency.getInstance("USD");//Japan Yen// Use of getCurrencyCode() methodString cCode1 = c1.getCurrencyCode();String cCode2 = c2.getCurrencyCode();System.out.println("Australian Dollar code : "+ cCode1);System.out.println("Japan Yen code : "+ cCode2);System.out.println("");// Use of getDefaultFractionDigits() methodintD1 = c1.getDefaultFractionDigits();System.out.println("AUD Fraction digits : "+ D1);intD2 = c2.getDefaultFractionDigits();System.out.println("JPY fraction digits : "+ D2);System.out.println("");// Use of getDisplayName() methodSystem.out.println("AUD Display : "+c1.getDisplayName());System.out.println("JPY Display : "+c2.getSymbol());System.out.println("");// Use of getSymbol() methodSystem.out.println("JPY Symbol : "+c2.getSymbol());System.out.println("USD Symbol : "+c3.getSymbol());}}Output:
Australian Dollar code : AUD Japan Yen code : JPY AUD Fraction digits : 2 JPY fraction digits : 0 AUD Display : Australian Dollar JPY Display : JPY JPY Symbol : JPY USD Symbol : $
Refer the link for list of ISO 4217 Currency Codes :
http://www.xe.com/iso4217.phpThis article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.




