# Formatting Decimal Numbers

DecimalFormat class which is extended from NumberFormat allows you to format a number (both decimal and integer) into a beautified String. It can be used to display in reports in general. DecimalFormat also helps to parse number from String literals.

It can be used in localized application also. That is you can use it based on a particular locale. It is one of the main highlight of this class. You can format a range of numbers like integers (1234), decimal numbers (1234.56), scientific notation (1.23E4), percentages (98%), and currency amounts (\$1234).

When formatting a number to a String, first you need to get an instance of the DecimalFormat. Then you can associate a pattern to that formatter. Using this format instance numbers can be formatted. The key here is learning about the pattern symbols available and how they can be used with one another.

Easiest way to learn this is by example. I have given a pretty long example source code to learn formatting. Following are detail of common pattern characters.

## Pattern characters for formatting numbers

0 – to signify a digit
# – digit or absence of a number
. – decimal point (you should use always . signify a decimal point in pattern, but the formatted character will be based on Locale
E – separates mantissa and exponent in scientific notation
, – digits group separator
% – multiplies the number by 100 and adds a ‘%’ at the end
‘ – to use a pattern character itself in a pattern. Just a escape character!

## Example Java Source Code For Number Formatting

```import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class DecimalFormatExample {

public static void main(String args[]) {

NumberFormat formatter;

// parsing a negative integer from String
formatter = new DecimalFormat("#,##0;(#,##0)");
try {
System.out.println(formatter.parse("(1,234)"));
} catch (ParseException e) {
}

// formatting to an integer from a decimal
// with a prefix of 0s
//uses default RoundingMode RoundingMode.HALF_EVEN
formatter = new DecimalFormat("00000");
System.out.println(formatter.format(-123.456));

// print a example number using different formats
// for the current Locale
Locale currentLocale = Locale.getDefault();
System.out.println("Format based on locale "
+ currentLocale.getDisplayName());
double exampleNumber = -1234.56;

for (int j = 0; j < 4; ++j) {
switch (j) {
case 0:
formatter = NumberFormat.getInstance(currentLocale);
break;
case 1:
formatter = NumberFormat.getIntegerInstance(currentLocale);
break;
case 2:
formatter = NumberFormat.getCurrencyInstance(currentLocale);
break;
default:
formatter = NumberFormat.getPercentInstance(currentLocale);
break;
}
String pattern = ((DecimalFormat) formatter).toPattern();
String formatted = formatter.format(exampleNumber);
System.out.print(pattern + " -> " + formatted);
try {
Number parsedNumber = formatter.parse(formatted);
System.out.println(" -> parsed back from String to Number "
+ parsedNumber);
} catch (ParseException e) {
}
}
// setting an explicit + sign for a positive number
// and using () for negative number
formatter = new DecimalFormat("+0.0;(0.0)");
System.out.println(formatter.format(1234.5));
System.out.println(formatter.format(-1234.5));
}
}
```

### Output For The Above Example Java Source Code For Number Formatting

```-1234
-00123
Format based on locale English (United States)
#,##0.### -> -1,234.56 -> parsed back from String to Number -1234.56
#,##0 -> -1,235 -> parsed back from String to Number -1235
¤#,##0.00;(¤#,##0.00) -> (\$1,234.56) -> parsed back from String to Number -1234.56
#,##0% -> -123,456% -> parsed back from String to Number -1234.56
+1234.5
(1234.5)
```

This Core Java tutorial was added on 19/10/2009.

## Comments on "Formatting Decimal Numbers" Tutorial:

1. Lyndsay says:

In what way DecimalFormat and NumberFormat are different?

2. Hina Agarwal says:

While surfing for some specific question I came across your site today, its been almost 2 hrs that I have been reading through it and still want to read further.
I appreciate the simplicity of language and the appropriate examples provided by you.All I can say is:
I’m loving It!!!

Comments are closed for this "Formatting Decimal Numbers" tutorial.