How to count the number of digits in an integer with Java

3 Answers

0 votes
public class DigitCounterLog10 {

    /**
     * Counts digits using a logarithmic formula.
     *
     * For any positive integer n:
     *     floor(log10(n)) + 1
     * gives the number of digits.
     *
     * Negative numbers are handled by converting to absolute value.
     * Zero is a special case because log10(0) is undefined.
     */
    public static int countDigits(int num) {
        // Convert negative numbers to positive
        num = Math.abs(num);

        // Zero must be handled explicitly
        if (num == 0) {
            return 1;
        }

        // Use logarithm base 10 to compute digit count
        return (int) Math.floor(Math.log10(num)) + 1;
    }

    public static void main(String[] args) {
        int value = 987654321;

        int digits = countDigits(value);

        System.out.println("Number: " + value);
        System.out.println("Digit count: " + digits);
    }
}


/*
run:

Number: 987654321
Digit count: 9

*/

 



answered Jun 4, 2020 by avibootz
edited Aug 13 by avibootz
0 votes
public class DigitCounter {

    /**
     * Returns the number of digits in an integer.
     *
     * This approach converts the number to a String and uses its length.
     * Modern JVMs optimize String operations very well, making this
     * both clear and efficient for everyday use.
     *
     * Handles negative numbers by stripping the leading '-' sign.
     */
    public static int countDigits(int value) {
        // Convert the integer to a string representation
        String text = String.valueOf(value);

        // If the number is negative, ignore the '-' sign
        if (text.charAt(0) == '-') {
            return text.length() - 1;
        }

        // Otherwise, the length of the string is the digit count
        return text.length();
    }

    public static void main(String[] args) {
        // Example input
        int number = 123456;

        // Compute the digit count using our function
        int digits = countDigits(number);

        // Display the result
        System.out.println("Number: " + number);
        System.out.println("Digit count: " + digits);
    }
}


/*
run:

Number: 123456
Digit count: 6

*/

 



answered Nov 6, 2023 by avibootz
edited Aug 13 by avibootz
0 votes
public class DigitCounterIterative {

    /**
     * Counts how many digits an integer has using repeated division.
     *
     * This method removes any leading minus sign by taking the absolute value.
     * It then divides the number by 10 until nothing remains.
     * Each division strips one digit from the right side.
     */
    public static int countDigits(int num) {
        // Convert negative numbers to positive for counting
        num = Math.abs(num);

        // Zero is a special case: it has exactly one digit
        if (num == 0) {
            return 1;
        }

        int count = 0;

        // Repeatedly divide by 10 until the number is gone
        while (num > 0) {
            num /= 10;   // Remove one digit
            count++;     // Track how many digits were removed
        }

        return count;
    }

    public static void main(String[] args) {
        int n = 123456;

        // Compute and display the digit count
        System.out.println("Number of digits: " + countDigits(n));
    }
}


/*
run:

Number of digits: 6

*/

 



answered Aug 13 by avibootz
...