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;
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());}}
¥
To get all the available currency codes, use the static getAvailableCurrencies()
method of the Currency
class.
System.out.println(Currency.getAvailableCurrencies());