Friday, February 10, 2012

Java NumberFormat tutorials: 3 Examples of NumberFormat in java

Basic Usage of NumberFormat in java


When we are developing any applications targeted to different countries, the presentation of the data shoulld be

specific to each country eventhough data storage is always same. This is tedious task for java programmers to handle of formating the same data to specific country. java introduced Globallization and localization concept. As part this there are number of formatting classes including NumberFormat are introduced in java.text package.

What is internalization(i18n) and localization in java?
internilization is a concept programming technique to code in sucha way that any software applicaitons works for multiple languages as well as countries. data from application should be displayed as per the locale specific.

Usually, considering financial applicaiton, the data stored in backend database is always same ,but the format for displaying is always different for each country. that means USA has dollar symbol as currency in representing any price of object and dollar always displayed before the price number ($100) Where as India has different symbol for currency and this symblo should be displayed after price of any number (100 Rs). So we have to write a applicaiton in sucha way that it supports multiple languages and multiple countries.

one more scenario is if we develope a web applicaiton that is targeted to different countries to display such data, we have to handle the different scenario as per below
the 1000000.11 number is stored in database and should have to display based on thousand separator as per specific country, that means, If i am accessing the applicaiton from USA, USA specific format should be displayed.

1,000,000.11 should be displayed for USA people
1 000 000,11 should be displayed for Genrmap people
The number stored is independent of any local, but the display should as per the locale

NumberFormat solves all above display of all the number formating capabilities as just mentioned above.

NumberFormat provides custom Format to format the numbers based on applicaiton needs

java provides API for solve all those display formats NumerFormat is one of the class in java.text, with these we can format the below cases.

1. Format the number as per the country and language
2. Format the numbers as per the currencies
3. Format the number percentages like thousand separator and precision

NumberFormat is abstract factory class which we can create objects using static getInstance() method Only

NumberFormat examples:- These examples are used to give basic idea of NumberFormat class or Subclass using custom format.

1. How to format a number to thousand separator?

We can do in many ways to format the format the number into thousand separator. One way is to use String.format() method 2. Using regular expression
and third way is using NumberFormat java class.

pasting the code using NumberFormat example.

Locale usaLocale = new Locale("en", "US");
Locale canadalocale = new Locale("fr", "CA");
NumberFormat formatusa = DecimalFormat.getInstance(usaLocale);
System.out.println("USA " + formatusa.format(123456789));
NumberFormat formatCanada = DecimalFormat.getInstance(canadalocale);
System.out.println("Canada " + formatCanada.format(123456789));

value is 123456789 and out put value should be separated with thousand separator and returned output 123,456,789

2. How to format the number to decimal places. or DecimalFormat example

DecimalFormat class is used to format the number to decimal places.NumberFormat is parent class for DecimalFormat. Mosttly decimal Format class is used to format the number specific to local settings

DecimalFormat df=new DecimalFormat("##.0000");
System.out.println("Decimal value with 4 digits "+df.format(12345.893477));

There are two things to note as we are providing pattern "##.0000" to DecimalFormat object
Format object format thes double/float number to 4 digits after decimal point.
This example also round the double value apply the decimal places for specific pattern.

3. How to display the number in currency format?

Locale englishLocale = Locale.US;
String numberDisplay = NumberFormat.getCurrencyInstance(englishLocale).format(89756.862);
Locale germanyLocale = Locale.GERMANY;
String numberDisplayInGermany = NumberFormat.getCurrencyInstance(germanyLocale).format(89756.862);
System.out.print(" "+numberDisplay+" "+numberDisplayInGermany);

and the output should be
$89,756.86 89.756,86 €

Hope you understood the basic usage of NumberFormat class in java . Please share your comments on this

Related article