NumberFormat getMinimumIntegerDigits() method in Java with Examples


The getMinimumIntegerDigits() method is a part of the java.text.NumberFormat class in Java. It returns the minimum number of digits allowed in the integer portion of a formatted number.

Syntax:

public int getMinimumIntegerDigits()

Example 1:

import java.text.NumberFormat;

public class Example {
   public static void main(String[] args) {
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumIntegerDigits(3);
      int num = 5;
      String formattedNum = nf.format(num);
      System.out.println(formattedNum);
   }
}

Output:

005

In the above example, we first create an instance of the NumberFormat class using the getInstance() method. We then set the minimum number of integer digits to 3 using the setMinimumIntegerDigits() method. Finally, we format the integer 5 using the format() method and print the result, which is 005.

Example 2:

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

public class Example {
   public static void main(String[] args) {
      NumberFormat nf = NumberFormat.getInstance(Locale.US);
      int num = 12345;
      String formattedNum = nf.format(num);
      int minIntDigits = nf.getMinimumIntegerDigits();
      System.out.println("Formatted Number: " + formattedNum);
      System.out.println("Minimum Integer Digits: " + minIntDigits);
   }
}

Output:

Formatted Number: 12,345
Minimum Integer Digits: 1

In the above example, we create an instance of the NumberFormat class for the US locale using the getInstance(Locale.US) method. We then format the integer 12345 using the format() method and store the result in the formattedNum variable. We also get the minimum number of integer digits using the getMinimumIntegerDigits() method and store the result in the minIntDigits variable. Finally, we print both the formatted number and the minimum integer digits. The output shows that the minimum integer digits is 1, which is the default value if not set explicitly.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.