NumberFormat isGroupingUsed() method in Java with Examples

The isGroupingUsed() method is a built-in method of the java.text.NumberFormat returns true is grouping is used in the given format, else it returns false. The grouping in English Locale for a number 567879 can be done as “5, 67, 879”, where the size if locale dependent and is determined by its subclass

Syntax:

public boolean isGroupingUsed()

Parameters: The function does not accepts any parameter.



Return Value: The function returns a boolean value, it returns true if grouping can be used in this format, else false.

Below is the implementation of the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
  
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
  
public class Main {
    public static void main(String[] args)
        throws Exception
    {
  
        // Get the instance for Locale US
        NumberFormat nF
            = NumberFormat
                  .getPercentInstance(
                      Locale.US);
  
        // Prints the hash-code value
        if (nF.isGroupingUsed())
            System.out.println("Yes!"
                               + " grouping is used");
        else
            System.out.println("No!"
                               + " grouping is used");
    }
}

chevron_right


Output:

Yes! grouping is used

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
  
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
  
public class Main {
    public static void main(String[] args)
        throws Exception
    {
  
        // Get the instance for Locale CANADA
        NumberFormat nF
            = NumberFormat
                  .getPercentInstance(
                      Locale.CANADA);
  
        // Prints the hash-code value
        if (nF.isGroupingUsed())
            System.out.println("Yes! "
                               + "grouping is used");
        else
            System.out.println("No! "
                               + "grouping is used");
    }
}

chevron_right


Output:

Yes! grouping is used

Reference: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#isGroupingUsed()




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.