Location-Based Currency Formatting in Java

Most of the applications today, which are targeted for a larger audience e.g. internet users, usually deal in money as well. In such applications, a requirement will be to display money/currency in a format specific to that location or country.

In this tutorial, I am giving some examples which will help you in displaying the location-specific currency in your application UI. I am first listing the classes used in examples, and then we will look at the real example codes.

Please note that NumberFormat class OR Currency class does not convert the currencies using exchange rate logic. They are plain representation according to the location data provided by Locale class. If you want to convert between currencies then add some more logic in your application.

1. Classes used in Currency Formatting

Below are the major java classes which are used to format locale-based currencies.

  • java.util.Locale : This class is used to get the location information of the end user which is currently using your application.
  • java.util.Currency : This class represents a currency. The class is designed so that there’s never more than one Currency instance for any given currency. Therefore, there’s no public constructor. We obtain a Currency instance using the getInstance() methods.
  • java.text.NumberFormat : NumberFormat helps you to format and parse numbers for any locale. We will use it’s getCurrencyInstance() method to get the currency number formatter.

Read More: How to get current user locale

2. Currency Formatting Examples

Let’s list down very basic uses of the above classes to display an amount into a country-specific currency name and format.

2.1. Current Locale

To get the current locale, use Locale.getDefault(). The following example has been run for US locale.

//This is the amount which we want to format
Double currencyAmount = new Double(123456789.555);

//Get current locale information
Locale currentLocale = Locale.getDefault();

//Get currency instance from locale; This will have all currency related information
Currency currentCurrency = Currency.getInstance(currentLocale);

//Currency Formatter specific to locale
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);

//Test the output
System.out.println(currentLocale.getDisplayName());       //English (United States)

System.out.println(currentCurrency.getDisplayName());     //US Dollar

System.out.println(currencyFormatter.format(currencyAmount));     //$123,456,789.56

Now let’s modify the locale to of France and observe the output:

2.2. Custom Locale

Let us run the above example in FR locale using the Locale.FRANCE constant.

//This is the amount which we want to format
Double currencyAmount = new Double(123456789.555);

//Using France locale
Locale currentLocale = Locale.FRANCE;

//Get currency instance from locale; This will have all currency related information
Currency currentCurrency = Currency.getInstance(currentLocale);

//Currency Formatter specific to locale
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);

//Test the output
System.out.println(currentLocale.getDisplayName()); //French (France)

System.out.println(currentCurrency.getDisplayName());   //Euro

System.out.println(currencyFormatter.format(currencyAmount));   //123 456 789,56 €

Easy enough. Isn’t it?

Happy Learning !!

Reference: http://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

8 thoughts on “Location-Based Currency Formatting in Java”

  1. Hi Lokesh,

    The Code which you provided is not working for Finland , India, Switzerland and lot many countries. Can i know is there any thing such that only some countries can will work

    Reply
  2. Hey, Thanks for such a good article. I am looking something like Custom Formatter available in .NET, like so exist in Java ? I am looking for totally custom formatter. If you know anything like this then please tell me.

    Reply
  3. Hi.
    I don’t know it is a good solution use currency in long life application. What if the currency of this state will change in future and you have no chance change the JDK of your application? (For example you app is deployed and customer can’t pay you for changes) Customers will expect actual currency and your application will depend on JDK version.

    Reply
    • Thanks for sharing your thoughts. Further, I believe that no application is life long application, at least history teaches us so. An application without upgrade for so long time just seems too much hypothetical me. And changing the currency of any nation is also not frequent event. It happen perhaps once in one (or more) decade(s).

      According to Java docs, Users can supersede the Java runtime currency data by creating a properties file named /lib/currency.properties. The contents of the properties file are key/value pairs of the ISO 3166 country codes and the ISO 4217 currency data respectively. The value part consists of three ISO 4217 values of a currency, i.e., an alphabetic code, a numeric code, and a minor unit. Those three ISO 4217 values are separated by commas.

      Ref: https://docs.oracle.com/javase/7/docs/api/java/util/Currency.html

      So, even if currency is changed to another currency then also we have some easy control to handle the scenario by changing a property file, which seems good enough for me.

      Reply

Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.