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 application, 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 location specific currency formatting in java.

I am first listing the classes used in examples, and then we will look at the real example codes.

Classes used in currency formatting in java

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

  1. java.util.Locale : This class is used to get the location information of the end user which is currently using your application.
  2. 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. You obtain a Currency instance using the getInstance methods.
  3. 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

Currency formatting examples

Let’s list down very basic uses of above classes to display a raw number into currency format.

Format according to US locale

import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;

public class NumberToCurrencyExamples
{
   public static void main(String[] args)
   {
      //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());
      
      System.out.println(currentCurrency.getDisplayName());
      
      System.out.println(currencyFormatter.format(currencyAmount));
   }
}

Output:

English (United States)
US Dollar
$123,456,789.56

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

Format according to FR locale

public static void main(String[] args)
{
  //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());
  
  System.out.println(currentCurrency.getDisplayName());
  
  System.out.println(currencyFormatter.format(currencyAmount));
}

Output:

French (France)
Euro
123 456 789,56 €

Easy enough. Isn’t it?

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.

That’s all for this very basic usecase examples of currency formatting in java.

Happy Learning !!

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

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

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

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.