Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

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:

  1. public static NumberFormat getCurrencyInstance(Locale locale)
  2. </pre></div>
  3. <a id=“currencyex”></a>
  4. <h3 class=“h3ex”>Example of Internationalizing Currency</h3>
  5. <p>In this example, we are internationalizing the currency. The format method of the NumberFormat class formats the double value into the locale specific currency.
  6. </p>
  7. <div class=“codeblock”><textarea name=“code” class=“java”>
  8. import java.text.NumberFormat;
  9. import java.util.*;
  10. public class InternalizationCurrency {
  11. static void printCurrency(Locale locale){
  12.  double dbl=10500.3245;
  13.  NumberFormat formatter=NumberFormat.getCurrencyInstance(locale);
  14.  String currency=formatter.format(dbl);
  15.  System.out.println(currency+” for the locale “+locale);
  16. }
  17. public static void main(String[] args) {
  18.     printCurrency(Locale.UK);
  19.     printCurrency(Locale.US);
  20.     printCurrency(Locale.FRANCE);
  21. }
  22. }
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

Comments

comments