programming
integer
digits
algorithms
math

How to find length of digits in an integer?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Finding the number of digits in an integer is easy once you decide what matters most: clarity, mathematical style, or avoiding string conversion. In ordinary application code, the clearest answer is usually to convert the absolute value to a string and take its length.

The simplest solution

python
1def digit_count(n: int) -> int:
2    return len(str(abs(n)))
3
4print(digit_count(12345))
5print(digit_count(-987))
6print(digit_count(0))

This handles:

  • positive integers
  • negative integers
  • zero

The abs call removes the minus sign so the count reflects only digits.

Why this is usually the best answer

The string-based solution is easy to read and hard to get wrong. In most applications, that matters more than avoiding a temporary string object.

If the goal is correctness and maintainability, this is usually the right default.

A mathematical approach with log10

For positive integers, the digit count is:

floor(log10(n)) + 1

python
1import math
2
3
4def digit_count_log(n: int) -> int:
5    n = abs(n)
6    if n == 0:
7        return 1
8    return math.floor(math.log10(n)) + 1

This is mathematically elegant, but it requires explicit handling for zero because log10(0) is undefined.

It is also less immediately readable for many developers than the string solution.

Integer-division loop approach

If you want to avoid both strings and logarithms, use repeated integer division.

python
1def digit_count_loop(n: int) -> int:
2    n = abs(n)
3    if n == 0:
4        return 1
5
6    count = 0
7    while n > 0:
8        n //= 10
9        count += 1
10    return count

This is straightforward and works entirely with integer operations.

Performance discussion

For small everyday integers, performance differences between these methods usually do not matter. The main distinction is readability.

That is why the string approach is so often the right recommendation in interview-prep aside: real code usually benefits more from obvious correctness than from avoiding a tiny temporary allocation.

  • string conversion is simplest
  • logarithms are concise but need edge-case care
  • loops are explicit and language-neutral

So the “best” method depends more on code context than on raw asymptotic complexity.

Edge cases

The key edge cases are:

These are small cases, but they are exactly the ones that make otherwise “obvious” one-line formulas fail when copied into production code without thought.

  • '0 should return 1'
  • negative numbers should ignore the minus sign
  • very large integers should still be handled correctly

The string and loop approaches handle arbitrary-size Python integers naturally.

Other languages

The same logic applies beyond Python. Many languages can solve this with:

So even if the syntax changes, the reasoning stays the same: choose the version that best balances correctness, clarity, and the constraints of the runtime.

  • string length
  • repeated division by 10
  • logarithms with care around zero and negative values

So the conceptual answer is language-independent even if the syntax differs.

Common Pitfalls

A common mistake is forgetting to handle 0, which should count as one digit.

This seems minor, but it is exactly the case that exposes whether a formula was copied mechanically or implemented with the real domain rules in mind.

Another mistake is applying log10 directly to negative values or zero.

A third mistake is forgetting that the minus sign is not a digit and should not be counted.

Summary

  • The clearest general solution is len(str(abs(n))).
  • Use a loop if you want an all-integer approach.
  • Use log10 only if you are comfortable handling its edge cases.
  • Always treat 0 as having one digit.
  • Ignore the minus sign when counting digits of negative integers.
  • Prefer the simplest approach unless you have a specific reason to avoid strings.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.