Currency class in Java- Get currency code , symbol using Locale in Java

Sharing is Caring

Java provides utility classes inside java.util package.java.util.Currency is a very interesting class in this package, it represents currencies defined in ISO 4217 by their currency codes. You can get currency codes, its symbol, default fraction digits using Currency class in Java.

java.lang.Object
  extended by java.util.Currency

See this short example, how we use Currency Class in Java, You can also get live currency fluctuation in java using Currency Converter API

package practiceJava;
import java.util.Currency;
import java.util.Locale;

public class CurrencyDemo {

public static void main(String[] args) {
// create a currency object with locale
Locale locale = Locale.US;
Currency curr = Currency.getInstance(locale);
System.out.println(“Locale’s currency code:” + curr.getCurrencyCode());
// Get symbol for Currecny
String symbol = curr.getSymbol();
System.out.println(“Symbol is :” + symbol);
// Get default fraction digit for Currecny
int frDigit = curr.getDefaultFractionDigits();
System.out.println(“Default fraction digit : ” + frDigit);
}
}

The output looks like this

Currency Class in Java
Cheers 🙂 Happy Learning

Related Posts

0 thoughts on “Currency class in Java- Get currency code , symbol using Locale in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *