Internationalizing Number (I18N with Number)

Internationalizing Number (I18N with Number)

  1. Internationalizing Number
  2. Example of Internationalizing Number

The representation of the numbers differ from one locale to another. Internationalizing the numbers is good approach for the application that displays the informations according to the locales.

The NumberFormat class is used to format the number according to the specific locale. To get the instance of the NumberFormat class, we need to call either getInstance() or getNumberInstance() methods.

Syntax of these methods is given below:

  1. public static NumberFormat getNumberInstance(Locale locale)
  2. public static NumberFormat getInstance(Locale locale)//same as above

Example of Internationalizing Number

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

  1. import java.text.NumberFormat;
  2. import java.util.*;
  3. public class InternalizationNumber {
  4. static void printNumber(Locale locale){
  5.  double dbl=105000.3245;
  6.  NumberFormat formatter=NumberFormat.getNumberInstance(locale);
  7.  String number=formatter.format(dbl);
  8.  System.out.println(number+” for the locale “+locale);
  9. }
  10. public static void main(String[] args) {
  11.     printNumber(Locale.UK);
  12.     printNumber(Locale.US);
  13.     printNumber(Locale.FRANCE);
  14.     printNumber(Locale.JAPAN);
  15. }
  16. }
download this example
Output:105,500.324 for the locale en_GB
       105,000.324 for the locale en_US
       105,a000,324 for the locale fr_FR
       105,000.324 for the locale ja_JP

Next TopicInternationalizing Currency
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Internationalizing Number (I18N with Number) Internationalizing Number Example of Internationalizing Number The representation of the numbers differ from one locale to another. Internationalizing the numbers is good approach for the application that displays the informations according to the locales. The NumberFormat class is used to format the number according to the specific locale. To get the instance […]

Tags:

....
...