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
Comments