Format Currency in Java

1. Introduction:

Many a times its required to format currency in java, especially an amount into a currency format based on user’s locale in case the application is being used globally. Say a user from USA would see the amount formatted in USD format along with the symbol, whereas a user from UK would see a GBP(Global British Pound) format along with the symbol.

To format a number to a particular currency, Java provides us an API called NumberFormat, we invoke getCurrencyInstance to get a format object. Calling  format() on this object will format the number based on the default locale (which is in US format). To format based on a different currency we shall pass the appropriate locale to the NumberFormat.getCurrencyInstance().

2. Program:

The below code demonstrates the concept explained above.

3. Output:

Currency_Format

Note: Observe the difference in currency formatting between France and German even though they both have Euro as currency.

Comments

  1. By vandana

    Reply

  2. By Harsh Jain

    Reply

  3. By rahul sagar

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *