java tutorial - Internationalizing Currency (I18N with Currency) - java programming - learn java - java basics - java for beginners



  • If we internationalize the date, time, numbers,and also internationalize the currency.
  • The currency differs from one place to another so we need to internationalize the currency.
  • The NumberFormat class provides methods to format the currency according to the locale.
  • The getCurrencyInstance() method of the NumberFormat class returns the instance of the NumberFormat class.

Syntax

public static NumberFormat getCurrencyInstance(Locale locale)  
click below button to copy the code. By - java tutorial - team

Example

  • In this example, we are internationalizing the currency.
  • The format method of the NumberFormat class formats the double value into the locale specific currency.
import java.text.NumberFormat;  
import java.util.*;  
public class I18NCurrency {  
  
static void printCurrency(Locale locale){  
 double num=17400.7878;  
 NumberFormat formatter=NumberFormat.getCurrencyInstance(locale);  
 String currency=formatter.format(num);  
 System.out.println("Currency "+currency+" and locale "+locale);  
}  
  
public static void main(String[] args) {  
    printCurrency(Locale.UK);  
    printCurrency(Locale.US);  
    printCurrency(Locale.FRANCE);  
}  
}  
click below button to copy the code. By - java tutorial - team

Output

Currency £17,400.79 and locale en_GB
Currency $17,400.79 and locale en_US
Currency 17 400,79 € and locale fr_FR

Related Searches to Internationalizing Currency (I18N with Currency)