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.

NumberFormat

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:

numberFormat.setRoundingMode(RoundingMode.HALF_DOWN);

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

 
numberFormat.setMinimumIntegerDigits(1); 
numberFormat.setMaximumIntegerDigits(5);

numberFormat.setMinimumFractionDigits(1); 
numberFormat.setMaximumFractionDigits(2);

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"));
System.out.println(numberFormat.format(111.999));
numberFormat = NumberFormat.getNumberInstance(new Locale("da", "DK"));
System.out.println(numberFormat.format(111.999));

The output will be:

111.999
111,999

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();
System.out.println(percentageFormat.format(0.55));

The output is:

 55% 

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")));
System.out.println(currencyFormat.format(100.44));

This will be:

$100.44

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"));
System.out.println(numberFormat.parse("115,012").intValue());

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"));
System.out.println(numberFormat.parse("115,012").intValue());

The output is:

115

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.

Similar Posts from the author:

2 thoughts to “Java NumberFormat in action

  • Essien

    Howdy! This post could not be written any better!

    Thank you for sharing!

  • Essien

    Howdy! This post could not be written any better!

    Thank you for sharing!

Comments are closed.