Java Currency Class Example

The currency class presents a currency method. The currency codes ISO 4217(International Organisation for standardization) is been identified for currency. This class presents all worldwide currencies. There is no public constructor as it is designed for only one currency instance at a time and not more than one. The # is used to begin the comment line.

also read:

Creating properties files named as /lib/currency.properties, the user can take over the java runtime currency data. The property file consists of two key value pairs i.e. ISO 3166 and ISO 4217. The ISO 4217 consist of 3 values i.e. numeric code, alphabetic code and minor unit.

Currency Class Declaration

public final class Currency extends Object implements Serializable

The above declaration defines an object of class currency which allows the serialization of the objects and extends the data from the class object. Serialization is an object can be converted to the series of bytes, which contains the information of the object’s data. Once serialized data has been written to the file, it can be deserialized to the object whenever required.

Currency Class Methods

MethodsDescription
Static Set getAvailableCurrencies( )This method sets the available currency.
String getCurrencyCode( )It is used to get the currency code i.e. ISO 4217.
String getSymbol( )It returns currency symbol for default locale.
String getSymbol(Locale locale )It returns currency symbol for specified locale.
String DisplayName( )It returns to display name of the currency for default locale.
String DisplayName(Locale locale )It returns to display name of the currency for specified locale. .
String toString( )It returns the currency code ISO 4217.
int getDefaultFractionDigits( )It is used to get default fraction digit number in the currency.
static Currency getInstance(Locale locale)It return’s the instance of currency for the specified locale.
static Currency getInstance(String currencyCode)It returns instance of currency for the specified currency code.

Currency Class Example

package myproject;

import java.util.Currency;
import java.util.*;

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

	      Locale loc = Locale.ITALY;
	      Currency c = Currency.getInstance(loc);

	      System.out.println("Currency code :" + c.getCurrencyCode());

	      String symbol = c.getSymbol();
	      System.out.println("Currency symbol is = " + symbol);

	      int usdDigits = c.getDefaultFractionDigits();
	      System.out.println("USD fraction digits:" + usdDigits);
	   }
}

  • In the above example we have used currency class and different currency methods i.e. getCurrencyCode( ), Currency getInstance(Locale locale), getDefaultFractionDigits() and getCurrencyCode() . We have defined symbol of currency code for country Italy.
  • Locale loc = Locale.ITALY; line specifies a particular locale.
  • Currency c = Currency.getInstance(loc); line gets the currency instance of the specified locale.
  • System.out.println(“Currency code :” + c.getCurrencyCode()); statement is used to get currency code for the specified locale.
  • String symbol = c.getSymbol( ); line shows the particular currency symbol for the specified locale.
  • int usdDigits = c.getDefaultFractionDigits(); line is used to get default fraction digit for currencies.

When you run the above example, you would get the following output:

Java CurrencyClass Example

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*