11 Digit Counting

There are many situations in which a program needs to know the number of digits in an int. Unfortunately, int variables don’t have properties other than their value. In other words, anthropomorphizing a little, int variables don’t know anything about themselves. This chapter considers solutions to this problem given this observation.


As you know from Chapter 3 on digit manipulation, in order to drop or extract digits from the left side of an integer, you needed to know the number of digits in the number. In the examples in that chapter, the number of digits was known. Unfortunately, that isn’t always the case. For example, credit card account numbers might have between six and nine digits. The problem, then, is that of determining the number of digits in a number.


As you also know from Chapter 3 on digit manipulation, the position of a digit corresponds to a particular power of 10. Specifically, the digit in position [latex]n[/latex] (counting from the right, starting with 0) corresponds to the [latex]10^n[/latex]s place. For example, position [latex]2[/latex] corresponds to the [latex]10^2[/latex] or [latex]100[/latex]s place. To count digits you need to be able to invert the process. For example, to count a three-digit number, you need to find the position that the [latex]100[/latex]s place corresponds to.

As you hopefully recall, this is the domain of the logarithm. In a decimal (i.e., base 10) representation, the [latex]\log_{10}(x)[/latex] is often described as the value of [latex]n[/latex] that [latex]10[/latex] must be raised to in order to get [latex]x[/latex]. More formally, [latex]\log_{10}(x)[/latex] is the value of [latex]n[/latex] that satisfies [latex]x = 10^n[/latex]. So, returning to our example, the position of the [latex]100[/latex]s place corresponds to [latex]\log_{10}(100)[/latex], which is [latex]2[/latex] (since [latex]10^2[/latex] is [latex]100[/latex]). More generally, [latex]\log_{b}(x)[/latex] is the value of [latex]n[/latex] that satisfies [latex]x = b^n[/latex], where [latex]b[/latex] is referred to as the base (or radix) of the logarithm.

Thinking About The Problem

Evaluating [latex]\log_{10}(x)[/latex] can be quite difficult, in general. However, it is easy to find bounds. For example, since [latex]\log_{10}(100)[/latex] is [latex]2[/latex] and [latex]\log_{10}(1000)[/latex] is [latex]3[/latex] (and logarithms are monotonic) it follows that [latex]\log_{10}(x)[/latex] is in the interval [latex][2, 3)[/latex] for any [latex]x \in [100, 1000)[/latex]. This means that the [latex]\log_{10}[/latex] of any three-digit number is in [latex][2, 3)[/latex] and that the [latex]\log_{10}[/latex] of any four-digit number is in [latex][3, 4)[/latex]. For example:

  • Math.log10(7198) evaluates to approximately 3.8572118423168926 which, as expected, is in the interval [latex][3, 4)[/latex].
  • Math.log10(462) evaluates to approximately 2.6646419755561257 which, as expected, is in the interval [latex][2, 3)[/latex].
  • Math.log10(10000) evaluates to 5.0 which, as expected, is in the interval [latex][5, 6)[/latex].

The Pattern

More generally, the [latex]\log_{10}[/latex] of any [latex]n[/latex]-digit number will be in the interval [latex][n-1, n)[/latex], which means it will be greater than or equal to [latex]n-1[/latex] and strictly less than [latex]n[/latex]. Hence, the integer part of [latex]\log_{10}[/latex] of any [latex]n[/latex]-digit number will be [latex]n-1[/latex]. So, the number of digits in the number x is given by:

    public static int digits(int x) {
        return (int) Math.log10(x) + 1;


Returning to the credit card example, suppose that the account number is as follows:

        cardNumber = 412831758;

Then, you can find the number of digits in the account number as follows:

        n = digits(cardNumber);

Now, as you know from Chapter 3, if you want to extract the left-most three digits (i.e., the issuer), you need to drop the rightmost n - 3 digits. This involves dividing by ten to the power of n-3, which you can implement as follows:

        issuer = cardNumber / (int) Math.pow(10.0, n - 3);

As another example, suppose you need to write a program for a newspaper that converts a dollar amount (e.g., a person’s annual income, the price of a house) into a phrase like “6 figures” or “7 figures”. You would again need to calculate the number of digits in the number of interest. So, for example, if you initialize the variable containing the annual income of the subject of the story as follows:

        income = 156720;

you can then figure out the number of “figures” in that variable as follows:

        figures = digits(income);

Looking Ahead

As with the digit manipulation pattern of Chapter 3, this pattern can be used with other representation schemes (i.e., other bases). All that is needed is to replace [latex]10[/latex] with the desired base, [latex]b[/latex].

Fortunately, it is easy to calculate the logarithm in one base given the logarithm in another. That is, assuming you have the ability to calculate [latex]\log_{10}(x)[/latex] (e.g., using Math.log10() in Java):

[latex]\log_{a}(x) = \frac{\log_{10}(x)}{\log_{10}(a)}[/latex]

whenever [latex]x > 0[/latex].

A Warning

Notice that the truncation pattern from Chapter 5 is not used in this pattern. Instead, typecasting is used to get the integer part of the value returned by Math.log10(). This is because Math.log10() returns a double, and the truncation pattern is for truncating integers.


Icon for the Creative Commons Attribution 4.0 International License

Patterns for Beginning Programmers Copyright © 2022 by David Bernstein is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book