C# Integer Digit Count (Get Number of Digits) Count the number of digits in an int with an optimized method.

Integer digit count. This method gets 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.

Example. We show 2 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. And: The second method, GetIntegerDigitCount, uses logic to determine how many characters the integer would have if it were a string. Note: Both methods count the sign as a digit (character). So -1 is considered to be 2 digits. We show 2 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. String Length

C# program that counts number of digits in integers 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

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

A summary. 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. 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. ToString

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.