Sunday, September 8, 2013

NumberFormat

NumberFormat is an abstract class that is useful to format a number. You can not create number format object directly from this class. Instead, you will use the getNumberInstance(), getCurrencyInstance(), and getPercentInstance() methods to format a general number, currency value, and percentage value respectively.
For example, the below code fragment formats a double number in the default locale.

NumberFormat nf=NumberFormat.getNumberInstance();
double num=987999.988;
String result=nf.format(num);
System.out.println(result);

The result from this code fragement is 987,999.988. When you format a number using the getNumberInstance() method, the thousand separator is taken in to account.

The another code fragment format a currency value in the default locale.

NumberFormat nf=NumberFormat.getCurrencyInstance();
double cur=987999.988;
String mycur=nf.format(cur);
System.out.println(mycur);

The result from this code fragment is $987,999.99. The thousand separator and currency symbol are included in the string representing the currency value. The number of digits after the decimal point (.) is two. If you want specify the number of digits after the decimal point, use DecimalFormat instead. For example, to format a number 12345.9123 to include the thousand separator and with two digits after the decimal point, you write the code below.

NumberFormat nf=NumberFormat.getInstance();
DecimalFormat df=(DecimalFormat)nf;
df.applyPattern("#,###.00");
double num=23024.9000;
String mynum=df.format(num);
System.out.println(mynum);

 To instruct the decimal formatter object to format a value according to your requirments, you will use the applyPattern(String pattern) method. The # symbol represents a digit. It treats zero digits as absent. The , symbol is a thousand separator. The 0 also represents a digit. However, it deos not treat zero digits as absent.

In the examples above, the format object is created to format numbers in the default locale. You can specify the locale for the format object by using getNumberInstance(Locale locale), getCurrencyIntance(Locale locale), and getPercentInstance(Locale locale) methods so that you can have a format object that can format number, currency, or percentage specific to your locale.
For example, the code fragment below formats a currency in French locale.

NumberFormat nf=NumberFormat.getCurrencyInstance(Locale.FRANCE);
double num=7444.9883;
String frCur=nf.format(num);
System.out.println(frCur);

The result from this code is 7 444,99 €. The , symbol is the decimal point and space is used as the thousand separator.

Besides formatting a number, the NumberFormat class can be used to convert or parse a string representing a number to a number.
The example code fragment below converts the string "123.9" to a number.

NumberFormat nf=NumberFormat.getNumberInstance();
try{
String numstr="123.9";
Number mynum=nf.parse(numstr);
double n=mynum.doubleValue();
System.out.println(n);
}catch(ParseException e){e.printStackTrace();
}

You must place the code that converts a string to number in try...catch block since a string might not represent a number so that the conversion fails and your progrm crashes. If you want to get the interger number instead of a double one, you can use intValue() instead of doubleValue() method.

No comments:

Post a Comment