cyberangles blog

Calculating the Number of Digits of a Decimal Number in Any Base

In the world of computer science and mathematics, we often encounter scenarios where we need to represent numbers in different bases. The most commonly used base is decimal (base 10), but bases like binary (base 2), octal (base 8), and hexadecimal (base 16) are also prevalent, especially in computing. One common problem that arises is determining the number of digits a given decimal number would have if it were represented in a different base. In this blog post, we will explore the mathematical concept behind this problem and provide various programming approaches to solve it.

2026-07

Table of Contents#

  1. Mathematical Concept
  2. Derivation of the Formula
  3. Python Implementation
  4. Java Implementation
  5. C++ Implementation
  6. Common Practices and Best Practices
  7. Example Usage
  8. Conclusion
  9. References

Mathematical Concept#

The number of digits (d) of a positive integer (N) in base (b) can be found using the following mathematical formula: [d = \lfloor\log_b(N)\rfloor + 1] where (\lfloor x \rfloor) represents the floor function, which rounds down the real number (x) to the nearest integer.

Derivation of the Formula#

Let's assume that a number (N) in base (b) is represented as (N=a_{n - 1}b^{n - 1}+a_{n - 2}b^{n - 2}+\cdots+a_1b^1 + a_0b^0), where (0\leq a_i < b) for (i = 0,1,\cdots,n - 1).

The smallest (n) - digit number in base (b) is (b^{n - 1}) (for example, the smallest 3 - digit decimal number is (10^{2}=100)), and the largest (n) - digit number in base (b) is (b^{n}-1) (for example, the largest 3 - digit decimal number is (10^{3}-1 = 999)).

So, if (N) is an (n) - digit number in base (b), we have the inequality (b^{n - 1}\leq N < b^{n}).

Taking the logarithm (base (b)) of all parts of the inequality, we get (\log_b(b^{n - 1})\leq\log_b(N)<\log_b(b^{n})).

Since (\log_b(b^{k})=k), the inequality becomes (n - 1\leq\log_b(N)<n).

Applying the floor function, we have (\lfloor\log_b(N)\rfloor=n - 1).

Finally, solving for (n), we get (n=\lfloor\log_b(N)\rfloor + 1).

Python Implementation#

import math
 
def num_digits_in_base(N, b):
    if N == 0:
        return 1
    return math.floor(math.log(N, b)) + 1

Java Implementation#

import java.lang.Math;
 
public class DigitCounter {
    public static int numDigitsInBase(int N, int b) {
        if (N == 0) {
            return 1;
        }
        return (int)(Math.floor(Math.log(N) / Math.log(b))) + 1;
    }
 
    public static void main(String[] args) {
        int N = 100;
        int b = 2;
        System.out.println(numDigitsInBase(N, b));
    }
}

C++ Implementation#

#include <iostream>
#include <cmath>
 
int numDigitsInBase(int N, int b) {
    if (N == 0) {
        return 1;
    }
    return static_cast<int>(std::floor(std::log(N) / std::log(b))) + 1;
}
 
int main() {
    int N = 100;
    int b = 2;
    std::cout << numDigitsInBase(N, b) << std::endl;
    return 0;
}

Common Practices and Best Practices#

  • Handle the case (N = 0) separately: The formula (\lfloor\log_b(N)\rfloor + 1) is not valid for (N = 0) because (\log(0)) is undefined. In most bases, the number (0) is represented as a single digit.
  • Use the change - of - base formula: Most programming languages provide a logarithm function for base (e) (natural logarithm). To calculate (\log_b(N)), we can use the change - of - base formula (\log_b(N)=\frac{\log(N)}{\log(b)}).
  • Error handling: When implementing the function, it's important to handle cases where the base (b) is less than 2, as bases less than 2 are not valid for number systems.

Example Usage#

Let's say we want to find the number of digits of the decimal number (N = 100) in base (b = 2).

Using the Python function:

N = 100
b = 2
print(num_digits_in_base(N, b))

The output will be 7, which means the decimal number 100 requires 7 bits (binary digits) to be represented in binary.

Conclusion#

Calculating the number of digits of a decimal number in any base is a fundamental problem in computer science and mathematics. By understanding the mathematical formula and implementing it correctly in different programming languages, we can efficiently solve this problem. Remember to handle edge cases such as (N = 0) and invalid bases to ensure the correctness of your code.

References#