Java 12: Compact Number Formatting

Introduction

Java 12 introduced compact number formatting as part of the java.text.NumberFormat class, allowing developers to format numbers in a concise and human-readable way. This feature is particularly useful for applications that need to display large numbers in a compact form, such as financial or statistical data. Compact number formatting is supported through the NumberFormat.Style class, which provides predefined styles for short and long formats.

Key Points:

  • Compact Formats: Allows large numbers to be displayed in a more readable format using suffixes like “K” for thousands, “M” for millions, etc.
  • Localization Support: Formats numbers according to the locale, ensuring the output is culturally appropriate.
  • Predefined Styles: Offers both short and long formats to suit different presentation needs.

Using Compact Number Formatting

To use compact number formatting, you need to use the NumberFormat class and specify the compact number format style and locale.

Basic Syntax

NumberFormat numberFormat = NumberFormat.getCompactNumberInstance(Locale locale, NumberFormat.Style style); 
  • locale: The locale for which the number format should be applied (e.g., Locale.US, Locale.INDIA).
  • style: The style of compact number formatting (NumberFormat.Style.SHORT or NumberFormat.Style.LONG).

Example: Compact Number Formatting

Let’s explore how to format numbers using compact number formatting with both short and long styles.

Example: Formatting Numbers in Short Style

import java.text.NumberFormat; import java.util.Locale; public class CompactNumberFormatExample { public static void main(String[] args) { // Define the number to be formatted long number = 1234567L; // Create a NumberFormat instance for compact number formatting (short style) NumberFormat shortFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); // Format the number using short style String formattedShort = shortFormat.format(number); // Display the formatted number System.out.println("Short Format: " + formattedShort); } } 

Output:

Short Format: 1M 

Explanation:

  • getCompactNumberInstance(): Creates a NumberFormat instance with compact number formatting in short style.
  • Short Format: The number 1,234,567 is formatted as 1.2M using short style.

Example: Formatting Numbers in Long Style

import java.text.NumberFormat; import java.util.Locale; public class CompactNumberFormatExample { public static void main(String[] args) { // Define the number to be formatted long number = 1234567L; // Create a NumberFormat instance for compact number formatting (long style) NumberFormat longFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG); // Format the number using long style String formattedLong = longFormat.format(number); // Display the formatted number System.out.println("Long Format: " + formattedLong); } } 

Output:

Long Format: 1 million 

Explanation:

  • getCompactNumberInstance(): Creates a NumberFormat instance with compact number formatting in long style.
  • Long Format: The number 1,234,567 is formatted as 1.2 million using long style.

Locale-Specific Compact Number Formatting

Compact number formatting is sensitive to locale, meaning that the formatted output can vary based on the locale settings. Let’s explore how compact number formatting behaves for different locales.

Example: Locale-Specific Formatting

import java.text.NumberFormat; import java.util.Locale; public class LocaleSpecificFormatExample { public static void main(String[] args) { // Define the number to be formatted long number = 123456789L; // Create NumberFormat instances for different locales NumberFormat usFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); NumberFormat indiaFormat = NumberFormat.getCompactNumberInstance(new Locale("en", "IN"), NumberFormat.Style.SHORT); NumberFormat germanyFormat = NumberFormat.getCompactNumberInstance(Locale.GERMANY, NumberFormat.Style.SHORT); // Format the number using different locales String formattedUS = usFormat.format(number); String formattedIndia = indiaFormat.format(number); String formattedGermany = germanyFormat.format(number); // Display the formatted numbers System.out.println("US Format: " + formattedUS); System.out.println("India Format: " + formattedIndia); System.out.println("Germany Format: " + formattedGermany); } } 

Output:

US Format: 123M India Format: 12Cr Germany Format: 123�Mio. 

Explanation:

  • Locale-Specific Formatting: The number 123,456,789 is formatted differently based on the locale settings.
    • US Format: Uses the M suffix for millions.
    • India Format: Uses the Cr suffix for crores (specific to Indian numbering).
    • Germany Format: Uses the Mio. suffix for millions and the comma as a decimal separator.

Customizing Compact Number Formatting

Java allows customization of compact number formatting through the DecimalFormat class. You can define custom patterns to suit specific formatting requirements.

Example: Custom Compact Number Formatting

import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; public class CustomFormatExample { public static void main(String[] args) { // Define the number to be formatted long number = 987654321L; // Create a DecimalFormat instance with a custom pattern DecimalFormat customFormat = (DecimalFormat) NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT); customFormat.applyPattern("0.0##E0"); // Format the number using the custom pattern String customFormatted = customFormat.format(number); // Display the custom formatted number System.out.println("Custom Format: " + customFormatted); } } 

Output:

Custom Format: 9.88E8 

Explanation:

  • Custom Pattern: The custom pattern "0.0##E0" is applied to format the number in scientific notation.
  • Customization: Allows for flexibility in how numbers are presented, beyond the standard compact number formats.

Conclusion

Java 12’s compact number formatting provides a convenient way to represent large numbers in a concise and human-readable format. By leveraging this feature, developers can create applications that present numerical data more effectively, considering both style and locale.

Summary:

  • Compact Formats: Use predefined styles for short and long formats.
  • Locale-Specific: Formats numbers according to the locale, adapting to cultural conventions.
  • Customization: Allows for custom patterns to achieve specific formatting needs.

By using compact number formatting, you can enhance the readability and usability of numerical data in your Java applications, making them more accessible to users worldwide.

Leave a Comment

Scroll to Top