Java Compact Number Formatting: Simplifying Large Number Display
Explore the compact formatting feature introduced in Java 12, which allows you to easily format large numbers—such as decimals, currency, or percentages—into shorter representations like 1K for 1000 or 1M for a million. This feature is ideal for displaying numbers in space-constrained areas and provides customization options for the strings used to represent these large values. Learn how to create an instance of CompactNumberFormat
using the built-in methods of NumberFormat
for different locales.
Java - Compact Number Formatting
Java 12 introduces a feature called compact formatting, which allows us to format large numbers such as decimals, currency, or percentages into short forms like 1K for 1000, 1M for a million, and so on. This feature is especially useful when displaying numbers in areas with limited space. We can also customize the strings used to display these large numbers.
Create a CompactNumberFormat Instance
To create an instance of CompactNumberFormat
for a locale, use the related built-in method of NumberFormat
.
Syntax
NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
In the example above, we create a formatter for the US locale with the short format style. This means 1000 will be represented as 1K. Similarly, you can create an instance for the long format:
Syntax
NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
In this case, 1000 will be represented as 1 thousand.
Format the Value
Once the formatter is created, we can use the format()
method to get the required formatted number string.
Syntax
// 1000 will be formatted as 1K
String formatted = formatter.format(1000);
// 1,000,000 will be formatted as 1M
formatted = formatter.format(1000000);
Example: Compact Number Formatting
The following example prints both long and short formats using compact number formatting.
Code
package com.tutorialsarena;
import java.text.NumberFormat;
import java.util.Locale;
public class Tester {
public static void main(String[] args) {
// Create the formatter instance for Long format
NumberFormat formatter = NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.LONG);
System.out.println("Long Formats");
System.out.println(formatter.format(1000));
System.out.println(formatter.format(1000000));
System.out.println(formatter.format(1000000000));
// Create the formatter instance for Short format
formatter = NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.SHORT);
System.out.println("Short Formats");
System.out.println(formatter.format(1000));
System.out.println(formatter.format(1000000));
System.out.println(formatter.format(1000000000));
}
}
Output
Long Formats
1 thousand
1 million
1 billion
Short Formats
1K
1M
1B
Compact Number Formatting and Fraction Digits
By default, the fraction digit is set to zero, but you can customize this by setting the minimum fraction digits. This can be achieved with the following method:
Syntax
NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
formatter.setMinimumFractionDigits(3);
// It will print 10.012K
System.out.println(formatter.format(10012));
Example: Compact Number Formatting with Fractions
In the following example, both long and short formats are printed with and without fraction digits.
Code
package com.tutorialsarena;
import java.text.NumberFormat;
import java.util.Locale;
public class Tester {
public static void main(String[] args) {
// Create the formatter instance for Short format
NumberFormat formatter = NumberFormat.getCompactNumberInstance(
Locale.US, NumberFormat.Style.SHORT);
System.out.println("Without using Fractions");
System.out.println(formatter.format(10012));
System.out.println(formatter.format(10000012));
// set the minimum 2 fraction digits to display
formatter.setMinimumFractionDigits(2);
System.out.println("Using Fractions");
System.out.println(formatter.format(10012));
System.out.println(formatter.format(10000012));
}
}
Output
Without using Fractions
10K
10M
Using Fractions
10.01K
10.00M