JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

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

[code lang=”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();

}
}
[/code]

Output…

[code]
——-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
[/code]

Filed Under: Java Tagged With: 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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved