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
- import java.text.NumberFormat;
- import java.util.*;
- public class NumberFormatExample {
- static void printNumber(Locale locale) {
- double d = 16397.1245;
- NumberFormat f = NumberFormat.getNumberInstance(locale);
- String num = f.format(d);
- System.out.println(num + " for " + locale);
- }
- public static void main(String[] args) {
- printNumber(Locale.US);
- printNumber(Locale.FRANCE);
- }
- }
Output
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.