java tutorial - Internationalizing Number (I18N with Number) - java programming - learn java - java basics - java for beginners
- The numbers differ from one place to another place.
- Internationalizing the numbers is good 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
public static NumberFormat getNumberInstance(Locale locale)
public static NumberFormat getInstance(Locale locale)//same as above
click below button to copy the code. By - java tutorial - team
Sample Code
- In this example, we are internationalizing the number.
- The format method of the NumberFormat class formats the double value into the locale specific number.
import java.text.NumberFormat;
import java.util.*;
public class I18NNumber {
static void printNumber(Locale locale){
double dbl=105000.3245;
NumberFormat formatter=NumberFormat.getNumberInstance(locale);
String number=formatter.format(dbl);
System.out.println(" Number "+number+" locale "+locale);
}
public static void main(String[] args) {
printNumber(Locale.FRANCE);
printNumber(Locale.JAPAN);
printNumber(Locale.UK);
printNumber(Locale.US);
}
}
click below button to copy the code. By - java tutorial - team
Output
Number 105 000,325 locale fr_FR
Number 105,000.325 locale ja_JP
Number 105,000.325 locale en_GB
Number 105,000.325 locale en_US