Compact Number Formatting

Learn to apply locale-sensitive compact/short number formatting to general purpose numbers e.g. decimal, currency, and percentage. It is added in Java 12 in CompactNumberFormat class.

For example, numbers like 1000 (for example) can be formatted as “1K” (short style) or “1 thousand” (long style).

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 formatted string can be displayed in that limited space.

A 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. Create new 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 customized number format as well where we can 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 array, but the first supplied pattern always corresponds to 100.
  • Based on number of array elements, these values for the range 100 to 1014.

2. Compact number formatting example

2.1 Simple formatting

Java program to format a number in compact number format.

import java.text.NumberFormat;
import java.util.Locale;

public class Main 
{
	public static void main(String[] args) 
	{
		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.

import java.text.NumberFormat;
import java.util.Locale;

public class Main 
{
	public static void main(String[] args) 
	{
		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. Compact number parsing example

Java program to parse compact number into long pattern.

import java.text.NumberFormat;
import java.util.Locale;

public class Main 
{
	public static void main(String[] args) throws Exception
	{
		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 in Java 12.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.