Currency class

In this chapter you will learn:

  1. How to create a country specific Currency
  2. Get the currency symbol
  3. Get the default fraction digits

Create a country specific Currency

The Currency class represents a currency.

static Currency getInstance(Locale localeObj) Returns a Currency object for the locale specified by localeObj.

import java.util.Currency;
import java.util.Locale;
/* jav a2 s  . c o m*/
public class Main {
  public static void main(String args[]) {
    Currency c = Currency.getInstance(Locale.US);
    System.out.println(c);

  }
}

The code above generates the following result.

Get the currency symbol

  • String getSymbol( )
    Returns the currency symbol (such as $) for the invoking object.
  • String getSymbol(Locale localeObj)
    Returns the currency symbol (such as $) for the locale passed in localeObj.
import java.util.Currency;
import java.util.Locale;
/*from  j a v  a2  s. c  om*/
public class Main {
  public static void main(String args[]) {
    Currency c = Currency.getInstance(Locale.US);
    System.out.println("Symbol: " + c.getSymbol());
  }
}

The output is shown here.

Fraction Digits

int getDefaultFractionDigits( ) Returns the number of digits after the decimal point that are normally used by the invoking currency. For example, there are 2 fractional digits normally used for dollars.

import java.util.Currency;
import java.util.Locale;
/*  j  a  v a 2s. com*/
public class Main {
  public static void main(String args[]) {
    Currency c = Currency.getInstance(Locale.US);
    System.out.println("Default fractional digits: " + 
         c.getDefaultFractionDigits());
  }
}

The output is shown here.

Next chapter...

What you will learn in the next chapter:

  1. What are the classes we use to do Regular Expression Processing
  2. What is Pattern class for
  3. What is Matcher class for
Home » Java Tutorial » Internationalization
Locale class
Locale country name and language name
ResourceBundle
Currency class