Localized Currency Formatting in Java

Most of the applications today, which are targeted at 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 that 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.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() method.
Currency usd = Currency.getInstance("USD");
System.out.println(usd.getDisplayName());  // Output: US Dollar
  • java.util.Locale: This class is used to get the location information of the end user who is currently using your application.
Locale usLocale = new Locale("en", "US");
  • java.text.NumberFormat: NumberFormat helps you to format and parse numbers for any locale. We will use its getCurrencyInstance() method to get the currency number formatted.
NumberFormat nf = NumberFormat.getCurrencyInstance();  //Default Locale
//or
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINESE);  //Cutom Locale

2. Formatting Currency

To format a numeric value as currency, use the format() method of the NumberFormat object. The following example has been run for US locale.

double amount = 1234.567;

NumberFormat localizedCurrencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
String formattedCurrency = localizedCurrencyFormat.format(amount);

System.out.println(formattedCurrency);  // Output: $1,234.57

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

double amount = 1234.567;

NumberFormat frCurrencyFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
String frFormattedCurrency = frCurrencyFormat.format(amount);

System.out.println(frFormattedCurrency);  // Output: 1 234,57 €

Easy enough. Isn’t it?

3. Parsing Currency

To parse a currency string back to a numeric value, use the parse() method of the NumberFormat object.

String currencyString = "$1,234.57";

try {
  NumberFormat localizedCurrencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
  Number parsedNumber = localizedCurrencyFormat.parse(currencyString);
  double parsedAmount = parsedNumber.doubleValue();
  System.out.println(parsedAmount);  // Output: 1234.57
} catch (ParseException e) {
  e.printStackTrace();
}

4. Handling Currency Round Off

The NumberFormat class allows you to specify the rounding mode for formatted currency values.

NumberFormat roundedCurrencyFormat = NumberFormat.getCurrencyInstance();
roundedCurrencyFormat.setRoundingMode(RoundingMode.HALF_UP);
String formattedValue = roundedCurrencyFormat.format(123456.789d);
System.out.println(formattedValue);  // Output: $123456.79

We can specify one of the following rounding modes for controlled precision and rounding behavior.

Rounding ModeDescriptionExample
UPRounds towards positive infinity. If the result is positive, it behaves like CEILING; if negative, it behaves like FLOOR.2.1 rounded UP is 3.0.
DOWNRounds towards negative infinity. If the result is positive, it behaves like FLOOR; if negative, it behaves like CEILING.2.9 rounded DOWN is 2.0.
CEILINGRounds towards positive infinity, even if the original number is negative.2.1 rounded CEILING is 3.0,
-2.1 rounded CEILING is -2.0.
FLOORRounds towards negative infinity. Always rounds towards negative infinity, even if the original number is positive.2.9 rounded FLOOR is 2.0,
-2.9 rounded FLOOR is -3.0.
HALF_UPRounds towards the nearest neighbor. If the number is exactly halfway between two neighbors, it rounds away from zero.2.5 rounded HALF_UP is 3.0
HALF_DOWNRounds towards the nearest neighbor. If the number is exactly halfway between two neighbors, it rounds towards the nearest even neighbor.2.5 rounded HALF_DOWN is 2.0
HALF_EVENRounds towards the nearest neighbor. If the number is exactly halfway between two neighbors, it rounds towards the nearest even neighbor.2.5 rounded HALF_EVEN is 2.0
UNNECESSARYIndicates that no rounding is necessary or should be performed. Any attempt to round will result in an exception.

5. Conclusion

Java provides comprehensive support for handling localized currency formats. This allows us to create applications that adapt to the preferences of users from different regions. By leveraging the NumberFormat, Currency, and Locale classes, developers can ensure that currency values are presented and parsed in a way that aligns with the cultural and linguistic expectations of users.

Happy Learning !!

Comments

Subscribe
Notify of
guest
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

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

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

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode