How to convert the currency code to the currency symbol in Java

This shot shows how to get the currency symbol, given the currency code.

For example, see below.

Currency Code Currency Symbol
INR
USD $
EUR

We can solve the problem with the Currency class in Java. The Currency class is defined in the util package in Java. First, you must import the util package before you can use the Currency class, as shown below.

import java.util.Currency;

Code

In the code below, we first define the currency code whose symbol is needed.

Then, we get the instance of the Currency class through the getInstance() method which passes the currency code.

Next we use the getSymbol() method on the currency object to get the currency symbol.

import java.util.Currency;
public class Main {
public static void main(String[] args) {
String currencyCode = "JPY";
Currency currency = Currency.getInstance(currencyCode);
System.out.println(currency.getSymbol());
}
}

Expected output

¥

To get all the available currency codes, use the static getAvailableCurrencies() method of the Currency class.

System.out.println(Currency.getAvailableCurrencies());