java.util.Currency Methods With Example

Last Updated : 2 Jun, 2025

In Java, the Currency class is present inside the java.util package. This class is used to represent and work with different international currencies. The Currency class provides various methods which are useful for getting the details of different kinds of currencies, such as their codes, symbols, and digits.

What is Currency Class?

This class allows developers to access currency-related data like ISO 4217 currency code, symbols, and display names.

Uses of Currency Class:

Applications like online stores, billing software, or accounting tools need to handle different currencies, and with the help of this class, we can manage money correctly based on the user's country. Let's now understand how we are going to use the currency class to display the currency code, symbol, and display name for the Indian Rupee.

Example: Basic Usage of Currency Class.

Java
// Java program to demonstrates the use of Currency class 
import java.util.*; 
import java.util.Currency;

public class Geeks {
    public static void main(String[] args) {
        // Create a Currency instance for Indian Rupee
        Currency currency = Currency.getInstance("INR");

        // Display the currency code
        System.out.println("Currency Code: " + currency.getCurrencyCode());

        // Display the currency symbol
        System.out.println("Symbol: " + currency.getSymbol());

        // Display the currency display name
        System.out.println("Display Name: " + currency.getDisplayName());
    }
}

Output
Currency Code: INR
Symbol: ₹
Display Name: Indian Rupee


Java Currency Methods

Now, we are going to discuss various methods of this class in details so that we can get better understanding.

1. getInstance(String currencyCode): This method returns a currency instance for the given ISO 4217 currency code.

Syntax:

public static Currency getInstance(String currencyCode)

  • Parameter: This method takes a single parameter currencyCode of string type represents the ISO 4217 currency code.
  • Return Type: This method returns a Currency object of the currencyCode passes as an argument.

2. getCurrencyCode(): This method return the ISO 4217 currency code of the Currency instance.

Syntax:

public string getCurrencyCode()

  • Parameter: This method does not take any parameter.
  • Return Type: It returns the currency code ISO 4217 i.e "INR" for indian rupees.

3. getDefaultFractionDigits(): This method returns the default number of decimal places used by the currency

Syntax:

public int getDefaultFractionDigits()

  • Parameter: This method does not take any parameter.
  • Return Types: This method return a integer value the fraction of digit used by currency with their respective decimal places.

4. getDisplayName(): This method returns the full name of the currency in a locale-sensitive manner.

Syntax:

public String getDisplayName()

  • Parameter: This method does not take any parameter.
  • Return Type: This method return a String value which is the localized name of the currency name.

5. getSymbol(): This method returns the symbol of the currency

public String getSymbol()

  • Parameter: This method does not take any parameter.
  • Return Type: This method returns a String which a symbol of the currency.


Example: Using different Currency Methods

Java
// Java program explaining Currency class methods 
// getInstance(), getCurrencyCode(),getDefaultFractionDigits() 
// getDisplayName(), getSymbol() 
import java.util.*; 

public class Geeks

{ 
	public static void main(String[] args) 
	{ 
		// Use of getInstance() method to 'AUD' instance 

		//Australian Dollar 
		Currency c1 = Currency.getInstance("AUD");

		 //Japan Yen  
		Currency c2 = Currency.getInstance("JPY");

		// Japan Yen
		Currency c3 = Currency.getInstance("USD"); 

		// Use of getCurrencyCode() method 
		String cc1 = c1.getCurrencyCode(); 
		String cc2 = c2.getCurrencyCode(); 

		System.out.println("Australian Dollar code : " + cc1); 
		System.out.println("Japan Yen code : " + cc2); 
		System.out.println(""); 

		// Use of getDefaultFractionDigits() method 
		int D1 = c1.getDefaultFractionDigits(); 
		System.out.println("AUD Fraction digits : " + D1); 

		int D2 = c2.getDefaultFractionDigits(); 
		System.out.println("JPY fraction digits : " + D2); 
		System.out.println(""); 

		// Use of getDisplayName() method 
		System.out.println("AUD Display : "+c1.getDisplayName()); 
		System.out.println("JPY Display : "+c2.getSymbol()); 
		System.out.println(""); 

		// Use of getSymbol() method 
		System.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 Symbol : ¥
USD Symbol : $


Note: These methods are useful in applications dealing with internationalization and currency formatting.

Comment
Article Tags: