«Back to Home

Core Java

Topics

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
  1. import java.text.NumberFormat;  
  2. import java.util.*;  
  3. public class CurrencyFormatExample {  
  4.     static void printCurrency(Locale locale) {  
  5.         double d = 20565.3546;  
  6.         NumberFormat f = NumberFormat.getCurrencyInstance(locale);  
  7.         String cur = f.format(d);  
  8.         System.out.println(cur + " for " + locale);  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         printCurrency(Locale.US);  
  12.         printCurrency(Locale.FRANCE);  
  13.     }  
  14. }  
8

Output

9

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.