C# Integer Digit Count

Numbers

This method finds the number of digits in an integer. It avoids the ToString method. It is possible to determine the number of digits by using an implicit cast and several if-conditions. This approach avoids all allocations.

Int

Example

We demonstrate two methods that both return the same result when passed integer arguments. The first method, GetIntegerDigitCountString, converts the integer to a string and then accesses the Length property on the string.

Note

The second method, GetIntegerDigitCount, uses mathematical logic and conditionals to determine how many digits and characters the integer would have if it were converted to a string. Both methods count the sign as a digit (character).

Program that counts number of digits in integers: C#

using System;

class Program
{
    static void Main()
    {
	// Write number of digits in the integers using two methods.
	Console.WriteLine(GetIntegerDigitCountString(int.MinValue));
	Console.WriteLine(GetIntegerDigitCount(int.MinValue));
	Console.WriteLine(GetIntegerDigitCountString(int.MaxValue));
	Console.WriteLine(GetIntegerDigitCount(int.MaxValue));
	Console.WriteLine(GetIntegerDigitCountString(0));
	Console.WriteLine(GetIntegerDigitCount(0));
	Console.WriteLine(GetIntegerDigitCountString(9999));
	Console.WriteLine(GetIntegerDigitCount(9999));
	Console.WriteLine(GetIntegerDigitCountString(-9999));
	Console.WriteLine(GetIntegerDigitCount(-9999));
    }

    static int GetIntegerDigitCountString(int value)
    {
	return value.ToString().Length;
    }

    static int GetIntegerDigitCount(int valueInt)
    {
	double value = valueInt;
	int sign = 0;
	if (value < 0)
	{
	    value = -value;
	    sign = 1;
	}
	if (value <= 9)
	{
	    return sign + 1;
	}
	if (value <= 99)
	{
	    return sign + 2;
	}
	if (value <= 999)
	{
	    return sign + 3;
	}
	if (value <= 9999)
	{
	    return sign + 4;
	}
	if (value <= 99999)
	{
	    return sign + 5;
	}
	if (value <= 999999)
	{
	    return sign + 6;
	}
	if (value <= 9999999)
	{
	    return sign + 7;
	}
	if (value <= 99999999)
	{
	    return sign + 8;
	}
	if (value <= 999999999)
	{
	    return sign + 9;
	}
	return sign + 10;
    }
}

Output

11
11
10
10
1
1
4
4
5
5
This section provides information

Here we explain how the GetIntegerDigitCount method is internally implemented. First, the int parameter is widened to a double value. This allows us to convert it to a positive value without losing information.

And:We test it against zero, and if it is negative, we set the sign variable to 1 and make it positive.

Then:We test against the constants (9, 99, 999) to determine its positive length. We return the sum of characters in the string.

Summary

The C# programming language

In this example, we computed the number of digits in an integer using mathematical logic only, and also by using the ToString method. The ToString version will force an allocation on the managed heap and is likely much slower.

And:Performance depends on the string implementation and garbage collection specifics.

ToString Method

C#: Number