-1

How can I find the amount of digits in an integer? Mathematically, and by using functions if there are any.

I don't really know how to do that, since I'm a somewhat beginner.

  • 3
    Mathematically it is floor(log10(abs(integer))) + 1 ... – Stephen C Jan 27 '23 at 08:54
  • well .. what did you try? On this site it's _required_ that to do some research before posting (the duplicate is one of many that came up in a quick search with a general purpose search engine). Please take the tour through the how-to-ask pages .. – kleopatra Jan 27 '23 at 12:47

4 Answers4

0

In Java, I would convert the integer to a string using the .toString() function and then use the string to determine the number of digits.

Integer digit = 10000;
Integer digitLength = abs(digit).toString().length();
bluaura
  • 21
  • 7
  • Correct, I did not think of the sign, thanks. You may need to check the length of the absolute value instead. – bluaura Jan 29 '23 at 16:15
0

Another option would be to do it iteratively by dividing number by 10, until result is 0.

int number = ...;
int count = 1;
while ((number /= 10) != 0) {
  count++;
}
Chaosfire
  • 4,818
  • 4
  • 8
  • 23
0

There are many ways to calculate the number of digits in a number. The main difference between them is how important performance is to you. The first way is to translate a number into a string and then take its length:

public static int countDigitsFoo(int x) {
    if (x == Integer.MIN_VALUE) {
        throw new RuntimeException("Cannot invert Integer.MIN_VALUE");
    }
    if (x < 0) {
        return countDigitsFoo(-x); // + 1; if you want count '-'
    }
    return Integer.toString(x).length();
}

This method is bad for everyone, except that it is easy to write. Here there is an extra allocation of memory, namely the translation of a number into a string. That with private calls to this function will hit performance very hard.

The second way. You can use integer division and sort of go by the number from right to left:

public static int countDigitsBoo(int x) {
    if (x == Integer.MIN_VALUE) {
        throw new RuntimeException("Cannot invert Integer.MIN_VALUE");
    }
    if (x < 0) {
        return countDigitsBoo(-x); // + 1; if you want count '-'
    }

    int count = 0;
    while (x > 0) {
        count++;
        x /= 10;
    }
    return count;
}

but even this method can be improved. I will not write it in full, but I will give part of the code. P.S. never use this method, it is rather another way to solve this problem, but no more

public static int countDigitsHoo(int x) {
    if (x == Integer.MIN_VALUE) {
        throw new RuntimeException("Cannot invert Integer.MIN_VALUE");
    }
    if (x < 0) {
        return countDigitsHoo(-x); // + 1; if you want count '-'
    }

    if (x < 10) {
        return 1;
    }
    if (x < 100) {
        return 2;
    }
    if (x < 1000) {
        return 3;
    }

    // ...

    return 10;
}

You also need to decide what is the number of digits in the number. Should I count the minus sign along with this? Also, in addition, you need to add a condition on Integer.MIN_VALUE because

Integer.MIN_VALUE == -Integer.MIN_VALUE

This is due to the fact that taking a unary minus occurs by -x = ~x + 1 at the hardware level, which leads to "looping" on -Integer.MIN_VALUE

pechhenka
  • 11
  • 3
  • Do you have any benchmarks to show the difference in performance between `ifs` and iterative division? Even if it faster, i can't imagine the performance increase being significant enough to justify writing large number `ifs` - what happens if the input might be a number with 100 digits for example. – Chaosfire Jan 27 '23 at 10:59
  • You can simplify the negative checks using `Math.abs(x)` to get the absolute value. – Chaosfire Jan 27 '23 at 11:00
  • @Chaosfire alas, I don't have benchmarks for this, I would probably say that of course a large number of conditions are unlikely to significantly improve performance. All I can argue is that the assembler division operation for the processor is more labor-intensive than the condition – pechhenka Jan 27 '23 at 18:18
  • I meant, that in the case of possibly large inputs, many `ifs` are required and it would not be justifiable from clean code point of view. – Chaosfire Jan 27 '23 at 19:31
  • this would not be very much since int has a maximum number of two billion in which 10 digits, that is, it would take only ten if. Also, all the powers of ten can be entered into an array and run through them in a loop – pechhenka Jan 28 '23 at 20:43
0

In this program we use a for loop without any body.

On each iteration, the value of num is divided by 10 and count is incremented by 1.

The for loop exits when num != 0 is false, i.e. num = 0.

Since, for loop doesn't have a body, you can change it to a single statement in Java as such:

for(; num != 0; num/=10, ++count);

public class Main {

  public static void main(String[] args) {

    int count = 0, num = 123456;

    for (; num != 0; num /= 10, ++count) {
    }

    System.out.println("Number of digits: " + count);
  }
}
Kavya Sree
  • 36
  • 4