• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Display Currency For A Locale In Java

January 28, 2014 //  by Krishna Srinivasan//  Leave a Comment

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

Category: JavaTag: Java Util

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.

Previous Post: « Servlet API
Next Post: How To Update Clipboard Content In Java »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact