Compact Number Formatting in Java: Simplifying Large Numbers for Readability

Java 12 introduced the Compact Number Formatting feature, making it easier to display large numbers in a readable and concise way, like transforming "1000" into "1K" or "1000000" into "1M". This feature is particularly useful in financial applications, dashboards, reports, and any UI where space is limited, and number readability is crucial.

In this article, we’ll cover how to use compact number formatting, explore different styles, and look at real-world examples in Java.

What is Compact Number Formatting?

Compact Number Formatting is a way to represent large numbers in a shorter format by abbreviating them (e.g., 1K, 1M, 1B) according to specified rules for a locale. This feature is useful in internationalized applications, as it adjusts automatically based on the user's language and region.

Java provides this functionality through the NumberFormat class, specifically with NumberFormat.getCompactNumberInstance(), introduced in Java 12.

Using Compact Number Formatting in Java

The NumberFormat.getCompactNumberInstance() method requires two parameters:

  1. Locale - The locale for which to format (e.g., Locale.US, Locale.UK, Locale.JAPAN).

  2. NumberFormat.Style - The style, which can be either SHORT (e.g., 1K) or LONG (e.g., 1 thousand).

Here's a basic example of using NumberFormat for compact number formatting:

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

public class CompactNumberExample {
    public static void main(String[] args) {
        NumberFormat compactFormatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);

        System.out.println(compactFormatter.format(1000));      // Output: 1K
        System.out.println(compactFormatter.format(2500000));   // Output: 2.5M
        System.out.println(compactFormatter.format(1000000000)); // Output: 1B
    }
}

Styles in Compact Number Formatting

Java supports two main styles for compact formatting:

  • SHORT: Displays abbreviated numbers, like "1K" or "1M".

  • LONG: Provides more descriptive output, like "1 thousand" or "1 million".

Example: SHORT vs. LONG Styles

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

public class CompactNumberStyles {
    public static void main(String[] args) {
        long number = 1500;

        NumberFormat shortFormatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
        NumberFormat longFormatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);

        System.out.println("SHORT: " + shortFormatter.format(number));  // Output: 1.5K
        System.out.println("LONG: " + longFormatter.format(number));    // Output: 1.5 thousand
    }
}

The choice of style often depends on the context. For example, a financial dashboard might prefer SHORT style for clarity, while a detailed report might benefit from LONG style for better readability.


Locale-Specific Formatting

Compact number formatting adapts to the locale you specify, making it helpful for internationalized applications. Let’s see how different locales represent the same number:

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

public class CompactNumberLocales {
    public static void main(String[] args) {
        long number = 1000000;

        NumberFormat usFormatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
        NumberFormat ukFormatter = NumberFormat.getCompactNumberInstance(Locale.UK, NumberFormat.Style.SHORT);
        NumberFormat jpFormatter = NumberFormat.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);

        System.out.println("US: " + usFormatter.format(number)); // Output: 1M
        System.out.println("UK: " + ukFormatter.format(number)); // Output: 1M
        System.out.println("Japan: " + jpFormatter.format(number)); // Output: 100万
    }
}

In this example:

  • Locale.US and Locale.UK represent one million as “1M”.

  • Locale.JAPAN represents one million as "100万," following local conventions.

This locale-specific formatting helps create more accessible applications for a global audience.


Real-World Examples of Compact Number Formatting

Example 1: Displaying Followers on a Social Media Platform

In social media applications, compact number formatting can be used to display follower counts in a clean format.

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

public class SocialMediaExample {
    public static void main(String[] args) {
        long followersCount = 1234000;

        NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
        System.out.println("Followers: " + formatter.format(followersCount)); // Output: Followers: 1.2M
    }
}

Example 2: Showing Sales Numbers in E-Commerce Dashboards

For e-commerce applications, displaying large sales numbers in compact format is essential for clear and concise dashboards.

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

public class SalesDashboardExample {
    public static void main(String[] args) {
        long monthlySales = 5400000;

        NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
        System.out.println("Monthly Sales: " + formatter.format(monthlySales)); // Output: Monthly Sales: 5.4M
    }
}

Formatting Decimal Places

By default, NumberFormat formats numbers with one decimal place if needed. However, you can customize this by setting a different rounding mode or maximum fraction digits:

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

public class DecimalPlacesExample {
    public static void main(String[] args) {
        NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
        formatter.setMaximumFractionDigits(0); // Set to no decimal places

        System.out.println(formatter.format(1500));     // Output: 2K
        System.out.println(formatter.format(2500000));  // Output: 3M
    }
}

Limitations and Considerations

  1. Only Available in Java 12+: Compact Number Formatting is only available in Java 12 and later versions. For earlier versions, you would need to create a custom formatting solution.

  2. Locale Variance: Make sure to test across different locales, as compact formatting varies based on locale-specific abbreviations.

  3. Performance with Large Data: For applications that need frequent number formatting (e.g., real-time dashboards), be mindful of performance. Caching instances of NumberFormat can improve efficiency.


Conclusion

Compact Number Formatting in Java makes it easy to display large numbers in a clear, space-efficient way, adapting to both local language and cultural conventions. With support for both short and long styles, it’s a valuable feature for applications with international users and data-driven interfaces. By mastering compact formatting, you can improve the readability and user experience of your Java applications.

More such articles:

https://medium.com/techwasti

https://www.youtube.com/@maheshwarligade

https://techwasti.com/series/spring-boot-tutorials

https://techwasti.com/series/go-language

0
Subscribe to my newsletter

Read articles from Maheshwar Ligade directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Maheshwar Ligade
Maheshwar Ligade

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI