«Back to Home

Core Java

Topics

Internationalize Number In Java

Internationalize Number
 
The representation of the numbers is different from one locale to another locale. Internationalizing the numbers is a good approach for the Application, which displays the information, according to the locales. The NumberFormat class formats the number, according to the particular locale. We can call either getInstance() or getNumberInstance() methods to get the object of the NumberFormat class.
 
Syntax of methods

public static NumberFormat getNumberInstance(Locale locale)
 
public static NumberFormat getInstance(Locale locale)
 
Let’s see an example of Internationalizing Number.
 
Code
  1. import java.text.NumberFormat;  
  2. import java.util.*;  
  3. public class NumberFormatExample {  
  4.     static void printNumber(Locale locale) {  
  5.         double d = 16397.1245;  
  6.         NumberFormat f = NumberFormat.getNumberInstance(locale);  
  7.         String num = f.format(d);  
  8.         System.out.println(num + " for " + locale);  
  9.     }  
  10.     public static void main(String[] args) {  
  11.         printNumber(Locale.US);  
  12.         printNumber(Locale.FRANCE);  
  13.     }  
  14. }  
6

Output

7

In the example, mentioned above, we internationalize the number and the format method of the NumberFormat class, which formats the double value into the locale particular number.
 
Summary

Thus, we learnt that the NumberFormat class formats the number, according to the particular locale and also learnt how to internationalize the number in Java.