Internationalize Currency In Java
Internationalize Currency
The currency is different from one country to another country. Thus, we need to internationalize the currency. The NumberFormat class gives the methods to format the currency, according to the locale. The getCurrencyInstance()method of the NumberFormat class is used to return the object of the NumberFormat class.
Syntax of the getCurrencyInstance() method
public static NumberFormat getCurrencyInstance(Locale locale)
Let’s see an example to internationalize the currency, given below.
Code
- import java.text.NumberFormat;
- import java.util.*;
- public class CurrencyFormatExample {
- static void printCurrency(Locale locale) {
- double d = 20565.3546;
- NumberFormat f = NumberFormat.getCurrencyInstance(locale);
- String cur = f.format(d);
- System.out.println(cur + " for " + locale);
- }
- public static void main(String[] args) {
- printCurrency(Locale.US);
- printCurrency(Locale.FRANCE);
- }
- }
Output
In the example, shown above, we internationalize the currency and the format method of the NumberFormat class formats the double value into the locale particular currency.
Summary
Thus, we learnt that the getCurrencyInstance()method of the NumberFormat class is used to return the object of the NumberFormat class and also learnt how to internationalize the currency in Java.