Way to get number of digits in an int?

To get the number of digits in an int in Java, you can use the log10() method of the Math class and then add 1 to the result. This will give you the number of digits in the integer, including leading zeros.

Here's an example of how to use log10() to get the number of digits in an int:

public class Main {
  public static void main(String[] args) {
    int num = 12345;
    int digits = (int) Math.log10(num) + 1;
    System.out.println("Number of digits: " + digits);  // Outputs: "Number of digits: 5"
  }
}

Note that the log10() method returns the base-10 logarithm of the number, so you need to add 1 to the result to get the actual number of digits.

You can also use the String.length() method to get the number of digits in an int, like this:

public class Main {
  public static void main(String[] args) {
    int num = 12345;
    String str = Integer.toString(num);
    int digits = str.length();
    System.out.println("Number of digits: " + digits);  // Outputs: "Number of digits: 5"
  }
}

This will convert the int to a string and then use the length() method to get the number of characters in the string, which is equivalent to the number of digits in the integer.