How To Display Currency For A Locale In Java

This example demonstrates how to get currency name for the given locale. This would be very useful for the programmers when you want to display the amount prefixed with the currency symbol dynamically for different countries. You can get the currency object for a given locale by calling Currency.getInstance(locale) method. This returns the currency object which has the following details:

  • Currency symbol – currency.getSymbol()
  • Currency code – currency.getCurrencyCode()
  • Fraction digits – currency.getDefaultFractionDigits()

Lets look at the example. It prints the default locale details and then setting the explicit language for that country. You can change to your country and try this example.

DisplayCurrencyExample.java

package javabeat.net.core;

import java.util.Currency;
import java.util.Locale;

public class DisplayCurrencyExample {
	public static void main(String[] args) throws Exception {
		Locale defaultLocale = Locale.getDefault();
		System.out.println("-------Displaying Default India Locale---------");
		System.out.println("Your Locale: " + defaultLocale.getDisplayName());
		Currency currency = Currency.getInstance(defaultLocale);
		System.out.println("Your Currency Code: " + currency.getCurrencyCode());
		System.out.println("Currency Symbol: " + currency.getSymbol());
		System.out.println("Default Fraction Digits For Your Currency: " + currency.getDefaultFractionDigits());
		System.out.println();

		System.out.println("-------Displaying India Locale Setting Hindi Language---------");

		Locale indiaLocale = new Locale("hi", "IN");

		System.out.println("Locale: " + indiaLocale.getDisplayName());
		currency = Currency.getInstance(indiaLocale);
		System.out.println("Currency Code: " + currency.getCurrencyCode());
		System.out.println("Symbol: " + currency.getSymbol());
		System.out.println("Default Fraction Digits For Your Currency: " + currency.getDefaultFractionDigits());
		System.out.println();

	}
}

Output…

-------Displaying Default India Locale---------
Your Locale: English (India)
Your Currency Code: INR
Currency Symbol: Rs.
Default Fraction Digits For Your Currency: 2

-------Displaying India Locale Setting Hindi Language---------
Locale: Hindi (India)
Currency Code: INR
Symbol: Rs.
Default Fraction Digits For Your Currency: 2

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!