Open In App

NumberFormat getCompactNumberInstance() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers. NumberFormat class also provides methods for determining which locales(US, Italy, etc) have number formats, and their names

NumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.

Class NumberFormat Hierarchy:

java.lang.Object
    java.text.Format
        java.text.NumberFormat

Constructor:

Constructor  Description
NumberFormat() Sole constructor. (For invocation by subclass constructors, typically implicit.)

Syntax:

public abstract class NumberFormat extends Format

getCompactNumberInstance() method:

Note: getCompactNumberInstance() method is added in Java 12. So please make sure that you have Java 12 or higher installed on your machine. You can check the java version by:

java -version

Type the above command in cmd and check the version. It should be above Java 12.  

Output

java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

Note: This method is added in Java 12. So please make sure that you have Java 12 or more installed on your machine. You can check the java version by the polling command as follows:

java -version

Type the above command in cmd and check the version. It should be above Java 12.  

Output:

java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

Here the Java version is 13.0.2.

Implementation:

Method 1: getCompactNumberInstance()

It is a built-in method of java.text.NumberFormat which returns a compact number format for the default FORMAT locale with “SHORT” format style. 

Syntax:

public static NumberFormat getCompactNumberInstance()

Parameters: This function does not accept any parameter.

Return Value: A NumberFormat instance for compact number formatting.

Example

Java




// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance() Method 
  
// Importing required classes
import java.util.*;
import java.text.NumberFormat;
import java.util.Locale;
  
// Main class
class GFG {
      
    // Main driver method  
    public static void main(String args[])
    {
        // Creating an object of NumberFormat class
        NumberFormat nf
            = NumberFormat.getCompactNumberInstance();
  
        // Print and display commands 
        System.out.println(
            "This method returns in NumberFormat.Style.SHORT:");
        System.out.println("Result: " + nf.format(100));
        System.out.println("Result: " + nf.format(10000));
        System.out.println("Result: " + nf.format(120300));
        System.out.println("Result: " + nf.format(2120000));
        System.out.println("Result: "
                           + nf.format(1240000300));
    }
}


Output:

This method returns in NumberFormat.Style.SHORT:
Result: 100
Result: 10K
Result: 120K
Result: 2M
Result: 1B

Method 2: getCompactNumberInstance​()

It is a built-in method of java.text.NumberFormat which returns  a compact number format for the specified locale and formatStyle. 

Syntax:

public static NumberFormat getCompactNumberInstance​(Locale locale, NumberFormat.Style formatStyle)

Parameters: This function accepts 2 parameters:

  • The desired locale
  • The style for formatting a number

Return Value: NumberFormat instance for compact number formatting

Exception: This method throws NullPointerException if locale or formatStyle is null

Example 1 

Java




// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance​() Method
  
// Importing required classes
import java.text.NumberFormat;
import java.util.Locale;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of NumberFormat class
        NumberFormat nfLong
            = NumberFormat.getCompactNumberInstance(
                Locale.US, NumberFormat.Style.LONG);
  
        // Print and display commands
        System.out.println(nfLong.format(100));
        System.out.println(nfLong.format(1000));
        System.out.println(nfLong.format(10000));
        System.out.println(nfLong.format(100000));
  
        // Again creating an object of NumberFormat class
        NumberFormat nfShort
            = NumberFormat.getCompactNumberInstance(
                Locale.US, NumberFormat.Style.SHORT);
  
        // Print and display commands
        System.out.println(nfShort.format(100));
        System.out.println(nfShort.format(1000));
        System.out.println(nfShort.format(10000));
        System.out.println(nfShort.format(100000));
    }
}


Output:

100
1 thousand
10 thousand
100 thousand
100
1K
10K
100K

Example 2

Java




// Java Program to Implement NumberFormat class by
// Illustrating getCompactNumberInstance​() Method
  
// Importing required classes
import java.text.NumberFormat;
import java.util.Locale;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args) throws Exception
    {
        // Creating an object of NumberFormat class
        NumberFormat fmt
            = NumberFormat.getCompactNumberInstance(
                Locale.US, NumberFormat.Style.LONG);
  
        // Print and display commands
        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"));
    }
}


Output:

100
1000
10000
100000

 Note: In the above program, we have used fmt.parse() method to parse compact numbers into LONG pattern.



Last Updated : 31 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads