Format a Decimal Number with DecimalFormat Example

When you need to format a decimal for example 2 decimals after the comma, grouping numbers, rounding decimals or adding a currency symbol. The java.text.DecimalFormat class is the way to go. The java.text.DecimalFormat allows us to specify a formatting pattern were we can format a decimal after the comma etc..

In the following examples we show you:

  • How to format a decimal number using different patterns.
  • How to format a decimal number using specific locale settings.
  • How to change the default decimal and grouping symbols.
  • How to configure number grouping.
  • How to display and configure the currency symbol.
  • How to use decimal rounding.

Format a Decimal Number Using Patterns

You can format a number using the DecimalFormat#format(number) method. The DecimalFormat takes a String pattern indicating how to format the decimal.

In first case we format a decimal using 4 grouping numbers and use the comma as a grouping separator. Show only 2 decimals after the comma and use the point as a decimal separator.

In the second case we format a decimal using 4 grouping numbers and use the comma as a grouping separator. Show always 3 digits after the comma even if there are no other digits to show and use the point as a decimal separator.

DecimalFormat df = new DecimalFormat("####,####.##");
System.out.println("Formatted decimal: " + df.format(12345.4567));

df.applyPattern("####,####.000");
System.out.println("Formatted decimal: " + df.format(12345.400));

The output printed from this code would be:

Formatted decimal: 1,2345.46
Formatted decimal: 1,2345.400

Format Decimal Number Using Specific Locale

The previous DecimalFormat was created in the default Locale of the JVM (computer) where to code is running on. You can however create a DecimalFormat which uses specific Locale settings. You can obtain a DecimalFormat using the factory method DecimalFormat#getNumberInstance(locale).

double amount = 123456789.4567;

DecimalFormat UK_DF = (DecimalFormat)DecimalFormat.getNumberInstance(Locale.ENGLISH);
DecimalFormat BE_DF = (DecimalFormat)DecimalFormat.getNumberInstance(Locale.GERMAN);

System.out.println("Formatted decimal (ENGLISH locale): " + UK_DF.format(amount));
System.out.println("Formatted decimal (GERMAN locale): " + BE_DF.format(amount));

You can see that we have used 2 different locales. The first (english) uses a comma as grouping separator and a point for decimal separator. The second (german) uses a point as grouping separator and a comma as decimal separator.

Formatted decimal (ENGLISH locale): 123,456,789.457
Formatted decimal (GERMAN locale): 123.456.789,457

How To Change the Decimal and Grouping Separator

We can change the grouping and decimal separators using the DecimalFormatSymbols class. With the DecimalFormatSymbols#setDecimalSeparator() method we can change the decimal separator and with the DecimalFormatSymbols#setGroupingSeparator() method we can change the grouping separator.

DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.getDefault());
formatSymbols.setDecimalSeparator(',');
formatSymbols.setGroupingSeparator(' ');

DecimalFormat df = new DecimalFormat("####,####.##", formatSymbols);

System.out.println("Formatted decimal: " + df.format(123456789.4567));

The output from this code will generate:

Formatted decimal: 1 2345 6789,46

Configure Number Grouping

After the DecimalFormat is created we can override the number grouping from the pattern by calling the grouping methods of the DecimalFormat. There are 2 methods for changing/configuring the grouping.

  • setGroupingSize(): this will set the size of the grouping used for number formatting.
  • setGroupingUsed(): this option wil enable or disable grouping.
double amount = 123456789.4567;
DecimalFormat df = new DecimalFormat("####,####.##");

System.out.println("Formatted decimal pattern: " + df.format(amount));

df.setGroupingSize(5);
System.out.println("Formatted decimal grouping size 5: " + df.format(amount));

df.setGroupingUsed(false);
System.out.println("Formatted decimal no grouping: " + df.format(amount));

The previous code will generate the following output:

Formatted decimal pattern: 1,2345,6789.46
Formatted decimal grouping size 5: 1234,56789.46
Formatted decimal no grouping: 123456789.46

Displaying and Configuring Currency Symbol

To display a currency symbol we can either provide a currency symbol in the pattern, or obtain a localized instance of the NumberFormat.

double amount = 1234.4567;

DecimalFormat euroFormat = new DecimalFormat("€#,##0.00");
System.out.println("Formatted euro currency: " + euroFormat.format(amount));

NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println("Formatted dollar currency: " + n.format(amount));

The previous code will generate the following output:

Formatted euro currency: €1,234.46
Formatted dollar currency: $1,234.46

Decimal Rounding

To format a decimal with specific decimal precision we can set a RoundingMode. These rounding mode correspond to the correct decimal precision that you want to obtain. Here you can find an overview of all available rounding modes.

double amount = 1234.4567;

NumberFormat n = DecimalFormat.getInstance();
System.out.println("Rounded decimal (default): " + n.format(amount));

n.setRoundingMode(java.math.RoundingMode.DOWN);
System.out.println("Rounded decimal (DOWN): " + n.format(amount));

n.setRoundingMode(java.math.RoundingMode.UP);
System.out.println("Rounded decimal (UP): " + n.format(amount));

The previous code will output the following:

Rounded decimal (default): 1,234.457
Rounded decimal (DOWN): 1,234.456
Rounded decimal (UP): 1,234.457

References

Download

You may also like...