Java NumberFormat tutorials: 3 Examples of NumberFormat in java
When developing applications for different countries, the data display should be customized to each country, even if the data storage is always the same. For java programmers, formatting the same data to a specific country is a tedious task.
java introduced the Globalization
and localization
concept. Many formatting classes, including NumberFormat
, are included in the java.text
package as part of this.
What is internalization and localization in java?
Internalization
is a programming approach for coding software applications in such a way that they work in different languages and countries.
The data from the application should be displayed according to the location and language
When it comes to financial applications, the data contained in the backend database is usually the same, but the format for displaying it varies by country. That is, the United States uses the dollar
symbol to indicate the price of an object, and the dollar
is always presented before the price number ($100), whereas India has a different symbol for currency, and this symbol is always displayed after the price number (100$). As a result, we must design an application that can support many languages and countries.
Let’s look at some other scenarios.
If we design a web application to display such data that is targeted to multiple countries, we must consider the following scenarios.
The 1000000.11 number is stored in the database and should be displayed using a thousand separators as applicable for each country. For example, if I access the application from the United States, the United States specific format should be displayed.
- 1,000,000.11 should be displayed for
USA
people - 1 000 000,11 should be displayed for
German
people
The number saved is independent of any locale
, however, the display should be updated accordingly.
NumberFormat class in java
resolves the above-mentioned display of all the number formatting capabilities.
NumberFormat class in java
NumberFormat
provides a custom Format for formatting numbers based on the need of the application.
Java has an API for dealing with all of these display formats. We can format the following cases using the NumberFormat
class from the java. text
package.
- Format the number as per the country and language
- Format the numbers as per the currencies
- Format the number percentages like thousand separators and precision
- How to display single digit in double digits always
- How to display percentage to input number with decimals
NumberFormat
is an abstract factory class which we can create objects using the static getInstance()
method Only
Let see examples using the NumberFormat
class
These examples are used to give a basic idea of the NumberFormat
class or Subclass using a custom format.
How to format or print a number to a thousand separator?
We can do in many ways to format the number into a thousand separator.
- One way is to use
String.format()
method - Second way Using
regular expression
- third way is using the
NumberFormat
java class.
Here is an example for NumberFormat example.
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class Test {
public static void main(String[] args) {
Locale usaLocale = new Locale("en", "US");
Locale canadalocale = new Locale("fr", "CA");
NumberFormat formatusa = DecimalFormat.getInstance(usaLocale);
formatusa.setGroupingUsed(true);
System.out.println("USA " + formatusa.format(123456789));
NumberFormat formatCanada = DecimalFormat.getInstance(canadalocale);
formatCanada.setGroupingUsed(true);
System.out.println("Canada " + formatCanada.format(123456789));
}
}
the input value is 123456789 and the output value should be separated with thousand separators and returned output 123,456,789
How to format the number to decimal places. or DecimalFormat example
DecimalFormat
class is used to format the number to decimal places.
NumberFormat
is the parent class for DecimalFormat
.
Mostly, the decimal Format class is used to format the number specific to local settings.
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
DecimalFormat df=new DecimalFormat("##.0000");
System.out.println("Decimal value with 4 digits "+df.format(12345.893477));
}
}
There are two things to note. We are providing pattern “##.0000” to DecimalFormat
object. Format object format these double/float numbers to 4 digits after the decimal point.
This example also rounds the double value and applies the decimal places for a specific pattern.
How to display the number in currency format?
It is an example to format the number in a specific country currency format.
It needs to set the Locale
class to be passed to NumberFormat Instance.
import java.text.NumberFormat;
import java.util.Locale;
public class Test {
public static void main(String[] args) {
Locale englishLocale = Locale.US;
String numberDisplay = NumberFormat.getCurrencyInstance(englishLocale).format(89756.862);
Locale germanyLocale = Locale.GERMANY;
String numberDisplayInGermany = NumberFormat.getCurrencyInstance(germanyLocale).format(89756.862);
System.out.print(" "+numberDisplay+" "+numberDisplayInGermany);
}
}
output:
$89,756.86 89.756,86 €
Hope you understood the basic usage of the NumberFormat
class in java.
How to display percentage to input number with decimals?
Sometimes, if the input number is 123.25, the output should display the number in terms of percentage i.e 12,325.00%
Here is a sequence of steps to do it
- First
NumberFormat
instance usinggetPercentInstance()
static method - if you want restrict return output with fraction of digits, use
setMinimumFractionDigits
- call
format
method with input value
import java.text.NumberFormat;
public class Test {
public static void main(String[] args) {
NumberFormat nfInstance = NumberFormat.getPercentInstance();
nfInstance.setMinimumFractionDigits(2);
System.out.println( nfInstance.format(123.25));
}
}
Output:
12,325.00%
How to display single digit in double digits always
For example, if the input is number any number from 1 to 90
if the input is 1 - outputs 01 if the input is 90 - outputs 90
- first create NumberFormat instance
getNumberInstance
method - restrict integer part of an value to 2 using
setMinimumIntegerDigits
- finally, format input number
import java.text.NumberFormat;
public class Test {
public static void main(String[] args) {
NumberFormat nfInstance = NumberFormat.getNumberInstance();
nfInstance.setMinimumIntegerDigits(2);
System.out.println( nfInstance.format(1));
}
}
Output
01
Conclusion
To Sum Up, You learned NumberFormat examples for formatting currency and number separation.