Learn to apply locale-sensitive compact/short number formatting to general-purpose numbers, e.g., decimals, currency, and percentages. It is added in Java 12 in CompactNumberFormat class.
With CompactNumberFormat, numbers like 1000 can be formatted as “1K” (short style) or “1 thousand” (long style). Similarly, we can use M for million and B for billion.
1. CompactNumberFormat class
CompactNumberFormat is a concrete subclass of NumberFormat
that formats a decimal number in its compact form. The compact number formatting is designed for the environment where the space is limited, and the screen can display the formatted string in that limited space.
Compact number formatting refers to the representation of a number in a shorter form based on the patterns provided for a given locale.
1.1. Creating a CompactNumberFormat Instance
To obtain a CompactNumberFormat
for a locale, use one of the factory methods given by NumberFormat
.
NumberFormat fmt = NumberFormat.getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT);
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
1.2. Custom CompactNumberFormat Instance
We can also create a customized instance and define how the numbers will be represented in shorter form using CompactNumberFormat(String, DecimalFormatSymbols, String[])
constructor.
final String[] compactPatterns
= {"", "", "", "0k", "00k", "000k", "0m", "00m", "000m",
"0b", "00b", "000b", "0t", "00t", "000t"};
final DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(Locale.GERMANY);
final CompactNumberFormat customCompactNumberFormat
= new CompactNumberFormat( decimalFormat.toPattern(),
decimalFormat.getDecimalFormatSymbols(), compactPatterns);
- The compact number compactPatterns are represented in a series of patterns where each pattern is used to format a range of numbers.
- Minimum one to maximum 15 patterns can be supplied in the array, but the first given pattern always corresponds to 100.
- Based on the number of array elements, these values range 100 to 1014.
2. Formatting a Number in Compact Format
2.1 Simple Formatting
In the following example, we are formatting the numbers first in the long and then in the short styles. We are using the US locale.
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
System.out.println( fmt.format(100) );
System.out.println( fmt.format(1000) );
System.out.println( fmt.format(10000) );
System.out.println( fmt.format(100000) );
NumberFormat fmtShort = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
System.out.println( fmtShort.format(100) );
System.out.println( fmtShort.format(1000) );
System.out.println( fmtShort.format(10000) );
System.out.println( fmtShort.format(100000) );
Program output.
100
1 thousand
10 thousand
100 thousand
100
1K
10K
100K
2.2. Set Fractional Number
Sets the minimum number of digits allowed in the fraction portion of a number. By default, fractional portion is set to '0'
digits.
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
fmt.setMinimumFractionDigits(3);
System.out.println( fmt.format(10000) );
System.out.println( fmt.format(10012) );
System.out.println( fmt.format(100201) );
System.out.println( fmt.format(1111111) );
Program output.
10.000K
10.012K
100.201K
1.111M
3. Parsing a Given Compact Number
Java program to parse compact numbers into a long pattern.
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
System.out.println( fmt.parse("100") );
System.out.println( fmt.parse("1 thousand") );
System.out.println( fmt.parse("10 thousand") );
System.out.println( fmt.parse("100 thousand") );
Program output.
100
1000
10000
100000
Drop me your questions related to compact number formatting.
Happy Learning !!
Leave a Reply