1. Overview

Counting the number of digits in a string or number is a common task that employs a few tricks to avoid miscounting or runtime errors.

In this tutorial, we’ll see some approaches to calculating the number of digits for a given value.

2. Implementation

Depending on the data type, we can apply certain techniques to analyze the value and get the number of digits. First, we’ll explore the implementation using strings.

2.1. Count Digits for Strings

The more general way to calculate the number of digits is by considering the values as Strings. For instance, we can take advantage of the isDigit() function available for Char objects:

fun CharSequence.countDigits(): Int = count { it.isDigit() }

Consequently, we can use this predefined method to get the digits count. Let’s verify that it works as expected:

@Test
fun `given a value when counting digits then number of digits should be returned`() {
    val number = 12345

    assertThat(number.toString().countDigits()).isNotNull().isEqualTo(5)
}

Additionally, if we want to have a more customized process, we can define the matching criteria for digits:

fun CharSequence.countDigitsCustom(): Int = when(this) {
    "" -> 0
    else -> this.count { it in ("0123456789") }
}

And we can process the calculation even for negative values:

@Test
fun `given a negative value when counting digits then number of digits should be returned`() {
    val number = -1234509

    assertThat(number.toString().countDigitsCustom()).isNotNull().isEqualTo(7)
}

2.2. Count Digits Using Regex

Another approach using Strings is to search for any character that matches a regular expression for digits:

fun CharSequence.countDigitsRegex(): Int = Regex("\\d").findAll(this).count()

Finally, we can test this approach for non-integer values:

@Test
fun `given a double value when counting digits then number of digits should be returned`() {
    val number = 1234.509

    assertThat(number.toString().countDigitsRegex()).isNotNull().isEqualTo(7)
}

2.3. Count Digits for Integers

If the goal is to use values in the realm of integers, we can define a more specific way to count digits using a well-known math formula. By applying the logarithm in base 10 of any number and rounding it up, we’ll get the number of digits in that number.

Taking advantage of the Kotlin support for logarithms, we can get the number of digits for any integer:

fun Int.countDigits() = when (this) {
    0 -> 1
    else -> log10(abs(toDouble())).toInt() + 1
}

3. Conclusion

In this tutorial, we’ve explored some efficient approaches to calculating the number of digits for a given value. We’ve learned that String objects come with great support to perform the search for digits. We have also seen how, by using clever mathematics, we can get the digit count of any integer.

As always, the complete code for this article is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.