I18N Tutor (Internationalization in Java)

This is a practical tutorial that helps in understanding Internationalization with numbers
Developed by Ranganathan Kaliyur Mannar (ranganathankm@gmail.com)


Understanding Number Formatter

Programs should operate on numbers in a locale-independent way. Before displaying or printing a number, a program must convert it to a String that is in a locale-sensitive format.

The methods of NumberFormat class can be used to format primitive-type numbers, such as double, and their corresponding wrapper objects, such as Double. The Usage is as follows:

NumberFormat.getInstance(localeObj).format(doubleVal);
or
NumberFormat.getInstance(localeObj).format(doubleWrapperObj);


Try out the following examples to understand the output:

1. Enter the number '98324.23' and Internationalize.


2. Change the Locale to French(France) by selecting 'fr' in Language,'FR' in Country and Internationalize.

Have a look at the number separators and decimal separator displayed based on the Locale.


3. Enter a bigger number '98310482945.2358' and Internationalize.

Try out this number with different locales...


4. Enter a negative number '-86453482945.3813' and Internationalize.

Note: The negative sign can also vary based on the locale.

Understanding Decimal Formatter


We can use the DecimalFormat class to format decimal numbers into locale-specific strings. This class allows us to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. If we want to change formatting symbols, such as the decimal separator, we can use the DecimalFormatSymbols in conjunction with the DecimalFormat class. These classes offer a great deal of flexibility in the formatting of numbers, but they can make our code more complex. This class is mainly to be used for attaining customized display patterns.

Usage:

DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(localeObj);
decimalFormat.applyPattern(patternStr);

decimalFormat.format(doubleVal);
or
decimalFormat.format(doubleWrapperObj);


Try out the following examples to understand the output:

1.
i.Enter the number '98324.23' in input field.
ii.Enter the pattern ###,###.### in pattern field.
iii.Internationalize


2. Change the Locale to French(France) by selecting 'fr' in Language,'FR' in Country and Internationalize.

Have a look at the number separators and decimal separator displayed based on the Locale. Importantly, even though the group separator has been mentioned as ',' and decimal separator has been mentioned as '.' in the pattern, these symbols have been displayed based on the locale in the output. This is because, the pattern which is based on U.S English conventions is not taken as hardcoded value, but clearly as patterns only.

Note: However, if we wish to use the pattern itself based on localized notation, the method applyLocalizedPattern should be used.


3.
i.Enter a bigger number '98310482945.2358' in input field.
ii.Enter the pattern ###,###.### in pattern field.
iii.Internationalize.

Take a look at the rounding done. Try out this example with different locales...


4.
i.Enter a bigger number '98310482945.2358' in input field.
ii.Enter the pattern ##,##,##.## in pattern field.
iii.Internationalize.

Take a look at how the grouping is done. The grouping is done based on the pattern we have specified and not based on the Locale settings.


5.
i.Enter a negative number '-86453482945.3813' in input field.
ii.Enter the pattern ###,###.### in pattern field.
iii.Internationalize.


6.
i.Enter the number '98324.23' in input field.
ii.Enter the pattern 000,000.000 in pattern field.
iii.Internationalize

Take a look at the leading and trailing zeros added. Try out this example with different locales...


7.
i.Enter the number '98310482945.2358' in input field.
ii.Enter the pattern 000,000.000 in pattern field.
iii.Internationalize

Take a look at the decimal part which is still rounded.


8.
i.Enter the number '98324.2329' in input field.
ii.Enter the pattern *****###,###.###*** in pattern field.
iii.Internationalize


9.
i.Enter the number '0.15' in input field.
ii.Enter the pattern '%' in pattern field.
iii.Internationalize

The '%' in pattern is used to display the output in percentage.

Understanding Currency Formatter


While writing business applications, we'll probably need to format and to display currencies. The methods of the Currency instance of the number formatter can be used to do this.

Usage:

NumberFormat.getCurrencyInstance(localeObj).format(doubleVal);
or
NumberFormat.getCurrencyInstance(localeObj).format(doubleWrapperObj);


Try out the following examples to understand the output:

1. Enter the number '98324.23' and Internationalize.

Have a look at the currency symbol displayed based on the Locale.


2. Change the Locale to French(France) by selecting 'fr' in Language,'FR' in Country and Internationalize.

Now, not only the currency symbol is changed to Euro, but it is also displayed after the amount. This happened because the Locale settings of French(France) define so.


3. Enter a bigger number '98310482945.2783' and Internationalize.

Now the number is formatted based on the locale's number formatter and the currency symbol.
Try out this number with different locales...


4. Enter a negative number '-86453482945.3813' and Internationalize.

Have a look at how a negative monetory amount is displayed.
Try out this number with different locales...


Note: When Locale is changed, the formatting and currency symbol change, but the amount remains the same. Currency formatter doesn't do exchange rate conversions!

Understanding Percent Formatter


The methods of the Percent instance of the NumberFormat class can be used to format percentages.

Usage:

NumberFormat.getPercentInstance(localeObj).format(doubleVal);
or
NumberFormat.getPercentInstance(localeObj).format(doubleWrapperObj);


Try out the following examples to understand the output:

1. Enter the number '0.2' and Internationalize.


2. Change the Locale to French(France) by selecting 'fr' in Language,'FR' in Country and Internationalize.


3. Enter another number '0.75' and Internationalize.