Java NumberFormat

Jakob Jenkov
Last update: 2014-06-23

The java.text.NumberFormat class is used to format numbers according to a specific Locale. Different countries have different standards for how they format numbers. In Denmark fractions of a number are separated from the integer part using a comma. In England they use a dot.

Creating a NumberFormat

Creating a NumberFormat for a specific Locale is done like this:

Locale locale = new Locale("da", "DK");

NumberFormat numberFormat = NumberFormat.getInstance(locale);

Formatting Numbers

Formatting a number using a NumberFormatter is done using the format() method. Here is an example:

String number = numberFormat.format(100.99);

System.out.println(number);

Using a Danish Locale, the output printed from this code would be:

100,99

Notice that numbers like 100.00 might be formatted without the decimals, as 100.

Formatting Currencies

To format a number as currency you need a currency NumberFormat instance. You create a currency NumberFormat using the getCurrencyInstance() like this:

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);

Formatting a number as a currency is still done using the format() method. Here is an example:

String currency = currencyFormat.format(100.999);

System.out.println(currency);

The output printed from this code would be:

kr 101,00

Notice how the localized Danish currency code is written in front of the number. Other countries might have the currency code after the number instead. Notice also that the amount is rounded.

It is also possible to obtain the Currency object representing the currency used to format the number. You do so using the getCurrency() method. Here is an example:

Currency currency = currencyFormat.getCurrency();

From the Currency instance you can get the name, symbol etc. of the currency, using its getDisplayName() and getCurrencyCode() etc. methods.

You can also set the Currency object to use for formatting a number as a currency, using the setCurrency() method on the NumberFormat class. Here is an example:

NumberFormat numberFormat =
    NumberFormat.getCurrencyInstance(new Locale("da", "DK"));
numberFormat.setCurrency(Currency.getInstance("EUR"));

String currencyString = numberFormat.format(120.99);
System.out.println(currencyString);

This code create a currency formatter using a Danish locale, then exchanges the currency to use for formatting with EURO. The output printed from this code would be:

€ 120,99

Formatting Percentages

To format a number as percentage you need a percentage NumberFormat instance. You create that using the getPercentageInstance() like this:

NumberFormat percentageFormat = NumberFormat.getPercentInstance(locale);

Formatting a number as a percentage is also done using the format() method. Here is an example:

String percentage = percentageFormat.format(99.999);

System.out.println(percentage);

The output printed from this code would be:

10.000%

Notice the % character after the number. Notice also that the number is rounded.

Minimum and Maximum Number of Digits

You can set both the minimum and maximum number of digits to use both for the integer part and the fractional part of the number. You do so using these methods:

numberFormat.setMinimumIntegerDigits(int digits)
numberFormat.setMaximumIntegerDigits(int digits)

numberFormat.setMinimumFractionDigits(int digits)
numberFormat.setMaximumFractionDigits(int digits)

You can also obtain these values again, using these methods:

int digits = numberFormat.getMinimumIntegerDigits();
int digits = numberFormat.getMaximumIntegerDigits();

int digits = numberFormat.getMinimumFractionDigits();
int digits = numberFormat.getMaximumFractionDigits();

Rounding Mode

As you may have noticed earlier, the formatted numbers are rounded. You can set the rounding mode using the setRoundingMode() method of the NumberFormat class. Here is an example:

NumberFormat numberFormat =
    NumberFormat.getInstance(new Locale("da", "DK"));

numberFormat.setRoundingMode(RoundingMode.HALF_DOWN);
numberFormat.setMinimumFractionDigits(0);
numberFormat.setMaximumFractionDigits(0);

String number = numberFormat.format(99.50);
System.out.println(number);

The output from this code would be 99. The fraction part would be rounded down because of the rounding mode, and to 0 fraction digits.

Parsing Numbers

You can also use the NumberFormat class to parse numbers. Here is an example:

NumberFormat numberFormat =
    NumberFormat.getInstance(new Locale("da", "DK"));

Number parse = numberFormat.parse("100,00");

System.out.println(parse.intValue());

The output printed from running this code would be:

100

As you can see, the number in the parsed string uses a comma (,) as separator between the integer part and the fraction part of the number. Since the NumberFormat is created using a Danish Locale instance, this string is parsed correctly to the value 100.

Had the NumberFormat instance been created using a UK Locale instance, the number had been parsed to 10000, because the comma would have been interpreted as a integer separator (e.g. thousand separator), and not a fraction separator.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next