Finding the number of digits of 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.
Introduction
Counting the number of digits in an integer is a common task in programming challenges, input validation, and number formatting. There are three standard approaches: converting to a string and measuring its length, using logarithms for a mathematical solution, and dividing by 10 in a loop. Each method has different performance characteristics and edge cases around zero and negative numbers.
Method 1: String Conversion
The simplest approach — convert the number to a string and count characters:
This method is readable and handles zero and negatives naturally. The trade-off is that it allocates a string, which is slower than pure arithmetic for performance-critical code.
Method 2: Logarithm (Base 10)
The number of digits in a positive integer n is floor(log10(n)) + 1:
The math behind this: log10(1000) = 3, so a 4-digit number like 1000 has floor(3) + 1 = 4 digits. This runs in O(1) time with no memory allocation.
Method 3: Repeated Division
Divide by 10 until the number reaches zero, counting iterations:
This is O(d) where d is the number of digits. It avoids floating-point issues entirely and works reliably with any integer size.
Method 4: Lookup Table (Fastest for Fixed-Width Integers)
For 32-bit integers, a precomputed table avoids both loops and floating-point math:
This runs in constant time (at most 10 comparisons for a 32-bit integer) with no floating-point operations.
Comparison of Methods
| Method | Time Complexity | Handles Zero | Handles Negatives | Floating-Point Issues |
| String conversion | O(d) | Yes | Use abs() | None |
| Logarithm | O(1) | Special case | Use abs() | Possible at boundaries |
| Division loop | O(d) | Special case | Use abs() | None |
| Lookup table | O(1) | Special case | Use abs() | None |
Handling Edge Cases
Language-Specific Notes
Common Pitfalls
- Forgetting zero:
log10(0)is negative infinity and a division loop on zero never executes. Both methods return incorrect results unless zero is handled as a special case (it has 1 digit). - Floating-point errors with logarithms:
log10(1000)might return2.9999999999999996instead of3.0, causingfloor()to give2instead of3. This happens at exact powers of 10 and varies by language and platform. - Integer overflow with
abs(): In Java and C,abs(Integer.MIN_VALUE)overflows back to a negative number because the positive equivalent exceeds the max value. Uselongor handleMIN_VALUEseparately. - Counting the minus sign:
str(-42)has length 3, but -42 has 2 digits. Always take the absolute value before counting. - Large integers and language limits: Python handles arbitrary-precision integers, but Java, C, and JavaScript have fixed-size integers. The string and division methods work across all sizes in Python, but the log method may overflow or lose precision in other languages.
Summary
- String conversion (
len(str(abs(n)))) is the simplest and most readable approach - Logarithm (
floor(log10(abs(n))) + 1) is O(1) but has floating-point edge cases at powers of 10 - Division loop (
n //= 10until zero) avoids floating-point issues and works with any integer size - Always handle zero as a special case (it has exactly 1 digit)
- Use
abs()to handle negative numbers, but watch for integer overflow in Java/C - For performance-critical code, a lookup table with threshold comparisons is the fastest approach
Related reading
- Finding the smallest set of rectangles that covers the given rectilinear simple polygons
- Finding the squares in a plane given n points
- finding the width of a binary tree
- Finding unreachable sections of a 2D map
- Finding whether a point lies inside a rectangle or not
- First appearance in Stern's Diatomic Sequence
- Fit multivariate gaussian distribution to a given dataset
- Fit rectangle around points

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 courseTrack 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.