Internationalizing Currency (I18N with Currency)

As we have internationalize the date, time and numbers, we can internationalize the currency also. The currency differs from one country 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.

The syntax of the getCurrencyInstance() method is given below:

snippet
public static NumberFormat getCurrencyInstance(Locale locale)

Example of Internationalizing Currency

In this example, we are internationalizing the currency. The format method of the NumberFormat class formats the double value into the locale specific currency.

snippet
import java.text.NumberFormat;
import java.util.*;
public class InternalizationCurrency {

static void printCurrency(Locale locale){
 double dbl=10500.3245;
 NumberFormat formatter=NumberFormat.getCurrencyInstance(locale);
 String currency=formatter.format(dbl);
 System.out.println(currency+" for the locale "+locale);
}

public static void main(String[] args) {
	printCurrency(Locale.UK);
	printCurrency(Locale.US);
	printCurrency(Locale.FRANCE);
}
}
Output
Output:£10,500.32 for the locale en_GB $10,500.32 for the locale en_US 10 500,32 £ for the locale fr_FR
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +