AlgoMaster Logo

Count Digits in an Integer

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

The number of digits in an integer is how many decimal symbols it takes to write, ignoring the sign. The value 12345 has 5 digits. The value -789 is written with three digit symbols and a minus sign, and the minus sign is not a digit, so the answer is 3.

Two cases need attention. The first is zero. 0 is written with the single symbol 0, so it has 1 digit. An approach that strips digits one at a time has to account for 0 directly, because there is no digit to strip.

The second is the most negative 32-bit value, -2147483648. Its absolute value is 2147483648, larger than the maximum positive int of 2147483647, so negating it or calling absolute value on it overflows back to a negative number. The safe options are to work in a wider type such as long, or to divide toward zero without negating. Integer division by 10 moves a negative number toward zero just as it does a positive one, so the loop terminates either way.

Key Constraints:

  • n can be zero → 0 is written as a single symbol, so it has 1 digit. Handle this as a separate case, since a strip-the-digits loop would otherwise count nothing and return 0.
  • n can be negative → The sign is not a digit. Count digits of the absolute value. Dividing a negative number by 10 still drives it toward 0, so a division loop works without negating the value.
  • n can be -2147483648 → Taking the absolute value of Integer.MIN_VALUE overflows a signed 32-bit int. Prefer dividing in a loop or widening to long before negating.

Approach 1: Repeated Division

Intuition

Dividing an integer by 10 shifts every digit one place to the right and discards the last. The number 12345 becomes 1234, then 123, then 12, then 1, then 0. The count of divisions it takes to reach 0 is the number of digits.

Negative numbers work the same way, since integer division rounds toward zero: -789 becomes -78, then -7, then 0. Only 0 produces no divisions, so it is handled before the loop.

Algorithm

  1. If n is 0, return 1.
  2. Set a counter to 0.
  3. While n is not 0, divide n by 10 and increment the counter.
  4. Return the counter.

Example Walkthrough

Input:

-789
n

The value -789 is not 0, so the loop runs. Dividing -789 by 10 gives -78 and the counter becomes 1. Dividing -78 by 10 gives -7 and the counter becomes 2. Dividing -7 by 10 gives 0 and the counter becomes 3. The number is now 0, so the loop stops and the answer is 3. The sign never affected the count, because division drove the value toward zero in three steps.

3
result

Code

Approach 2: Convert to String

Intuition

The number of digits is the count of digit characters in the printed form. Converting the integer to a string gives that form directly, and its length is the digit count once any minus sign is removed.

Taking the absolute value before converting strips the sign, but it overflows on Integer.MIN_VALUE. Converting first and then dropping a leading - avoids that.

Algorithm

  1. Convert n to its string form.
  2. If the string starts with -, remove that leading character.
  3. Return the length of the remaining string.

Example Walkthrough

Input:

-789
n

Converting -789 to a string produces "-789", which has length 4. The first character is -, so it is stripped, leaving "789" with length 3. The result is 3, matching the count from the division approach.

3
result

Code

Approach 3: Logarithm

Intuition

The base-10 logarithm answers the question "what power of 10 is this number close to," and that maps directly onto digit count. A number with d digits sits in the range from 10^(d-1) up to 10^d - 1. Taking log10 of any value in that range gives a result between d-1 and just under d. So floor(log10(|n|)) + 1 recovers the digit count for any non-zero number.

Zero has no logarithm, so it is handled as a separate case returning 1. This approach computes the answer with no loop, but it relies on floating-point math, which carries a precision caveat described below.

Algorithm

  1. If n is 0, return 1.
  2. Take the absolute value of n.
  3. Compute floor(log10(|n|)) + 1.
  4. Return that value.

Example Walkthrough

Input:

12345
n

The value 12345 is not 0. Its absolute value is 12345, and log10(12345) is about 4.09. Taking the floor gives 4, and adding 1 gives 5. The number has five digits, which matches. The reason the floor lands on 4 is that 12345 sits between 10^4 (10000) and 10^5 (100000), so its logarithm falls between 4 and 5.

5
result

A note on precision: log10 runs in floating point, and exact powers of 10 can land just below the true value. For example, log10(1000) might compute as 2.9999999 instead of 3.0, which would make the floor 2 and produce a wrong count. The division and string approaches avoid this entirely. When using the logarithm form, the values near exact powers of 10 are where errors appear, so the division approach is the safer default for this problem.

Code