Java Currency Class



Introduction

The Java Currency class represents a currency. Following are the important points about Currency −

  • Currencies are identified by their ISO 4217 currency codes.

  • The class is designed so that there's never more than one Currency instance for any given currency, this is the reason behind no public constructor.

Class declaration

Following is the declaration for java.util.Currency class −

public final class Currency
   extends Object
   implements Serializable

Class methods

Sr.No. Method & Description
1 static Set<Currency> getAvailableCurrencies()

This method gets the set of available currencies.

2 String getCurrencyCode()

This method gets the ISO 4217 currency code of this currency.

3 int getDefaultFractionDigits()

This method gets the default number of fraction digits used with this currency.

4 String getDisplayName()

This method gets the name that is suitable for displaying this currency for the default DISPLAY locale.

5 static Currency getInstance(Locale locale)

This method returns the Currency instance for the country of the given locale.

6 int getNumericCode()

This method returns the ISO 4217 numeric code of this currency.

7 String getNumericCodeAsString()

This method returns the 3 digit ISO 4217 numeric code of this currency as a String.

8 String getSymbol()

This method gets the symbol of this currency for the default locale.

9 String toString()

This method returns the ISO 4217 currency code of this currency.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Getting All Available Currency Codes Example

The following example shows the usage of Java Currency getAvailableCurrencies() method to get a set of available currencies. We've created a set of avaiable currencies using getAvailableCurrencies() method and then printed each currency code.

package com.tutorialspoint;

import java.util.Currency;
import java.util.Set;

public class CurrencyDemo {
   public static void main(String args[]) {

      Set<Currency> currencies = Currency.getAvailableCurrencies();

      System.out.println("Available Currencies: ");
      currencies.forEach(c -> System.out.print(c.getCurrencyCode() + " "));
   }
}

Let us compile and run the above program, this will produce the following result −

Available Currencies: 
SDG CNY TJS TMM IRR RWF KMF ZWR BYN BRL SYP SSP XTS BOV IQD TPE THB TND BND UYI LKR MGF MMK CHW MNT HKD ZMK SRD SKK CUP MYR SGD RUR SCR VES XPF RSD XCD XAU XOF WST DOP NIO LRD DJF TOP KPW GEL XFO MWK JPY KYD MKD XAG YER CRC XDR PLN RON KRW ANG RUB AED ESP XBA TWD ZAR TZS MUR HUF AZN MGA SBD XPT MTL UAH AOA TTD CVE HNL GBP BIF EEK SRG IDR MDL BGL NLG CHF FJD MOP LVL CLP HTG SIT KWD XUA AYM BYR FRF XXX PYG MXV MRO BDT COU SOS GNF ZWN VEF ATS BSD UZS BZD NPR IEP PAB DEM GMD UYU UGX CZK CDF LBP ALL PKR BTN XBD ETB DZD KGS STD LAK AUD QAR VND BHD JMD KHR SEK MZM SVC PHP USS TRL XAF SAR CYP BWP BGN CSD AFA MAD LTL TRY VUV SLL ARS PEN MVR GWP PTE ILS BYB CLF XPD AWG LYD ROL VEB MXN AMD OMR GIP CAD GHS FIM ZWD KZT JOD HRK DKK PGK YUM CUC LSL NGN ZWL AFN ZMW EGP NZD COP AZM BMD GHC GRD NAD BEF XSU TMT MRU XBB BAM CHE SDD BBD EUR NOK SHP SZL BOB LUF GTQ KES FKP ADP USN ISK MZN INR STN ITL XFU GYD ERN USD XBC 
Advertisements