Parsing or formatting various decimal or currency formats often make the job more difficult. If the expected number format is not the same as it is given then you as a programmer have to adapt to the situation and this is not always easy. There is an already implemented way in Java to handle this.


A NumberFormat object could be instantiate on a few way, depending on the situation.
NumberFormat numberFormat = NumberFormat.getNumberInstance();
NumberFormat percentageFormat = NumberFormat.getPercentInstance();
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
//Same as NumberFormat.getNumberInstance();
NumberFormat generalFormat = NumberFormat.getInstance();

Beside that they could be created with localization, with a Local object which is passed like a parameter.
Locale usaLocal = new Locale("en", "US");
NumberFormat numberFormat = NumberFormat.getNumberInstance(usaLocal);

The Locale object contains variables related to the selected country based on ISO standard which are available here.

NumberFormat allows to set up various rounding methods. RoundingMode could be specified with per-defined values and it is an easy and flexible way to handle the future changes. For instance, in this case the number with .5 fraction will be rounded down:


It is also possible to organize the maximum and minimum number of digits in the integer and the fraction part of the number:



The number formatting is simple with using Local objects. The example below shows that the output string could be different depending on selected localization. With US Locale object the decimal separator will be ‘.’ meanwhile the DK one results ‘,’ separator.

numberFormat = NumberFormat.getNumberInstance(new Locale("en", "US"));
numberFormat = NumberFormat.getNumberInstance(new Locale("da", "DK"));

The output will be:


If the NumberFormat is created as a percentage instance it will easily format the fraction part of the number as percentage output. Just like that:

percentageFormat = NumberFormat.getPercentInstance();

The output is:


On a very similar way could currency formats be handled. Just create a currency instance with the required localization and the formater will do the rest of the job.

currencyFormat = NumberFormat.getCurrencyInstance();
currencyFormat.setCurrency(Currency.getInstance(new Locale("en", "US")));

This will be:


Parsing strings to get the required types with NumberFormat also can be achieved with localizations. As it is mentioned before a US local gives period as decimal separator, however DK local will use comma for the same purpose.

numberFormat = NumberFormat.getInstance(new Locale("en", "US"));

The example above will result 115012 as an integer number. If the localization changed to DK the output will change on the following way:

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

The output is:
The ‘,’ was treated as decimal separator but only the integer value was requested so the result is correct.

I hope that will help you guys.

Posted By Árpád Farkas

    One Response to “Java NumberFormat in action”

  1. Essien says:

    Howdy! This post could not be written any better!

    Thank you for sharing!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>