Home
Map
Integer Digit CountCount the number of digits in an int with an optimized method.
C#
This page was last reviewed on May 18, 2023.
Integer digit count. The number 100 has 3 digits. A C# method can gets the number of digits in an integer like this one. We can avoid the ToString method.
Method info. It is possible to determine the number of digits by using an implicit cast and several if-conditions. This approach avoids all allocations.
if
Input and output. Consider a number like 1—it has 1 digit and this is easy to determine. But for 100, or -100, we want to get the numbers 3 and 4.
1 -> 1 100 -> 3 -100 -> 4
Example. We show 2 methods that both return the same result when passed integer arguments. Both methods count the sign as a digit (character).
Version 1 The first method converts the integer to a string and then accesses the Length property on the string.
String Length
Version 2 The second method, GetIntegerDigitCount, uses logic to determine how many characters the integer would have if it were a string.
using System; class Program { static void Main() { // Write number of digits in the integers using two methods. int[] values = {1, 100, -100}; foreach (int i in values) { int result1 = GetIntegerDigitCountString(i); int result2 = GetIntegerDigitCount(i); Console.WriteLine(i + "=" + result1 + " " + result2); } } static int GetIntegerDigitCountString(int value) { // Version 1: get digit count with ToString. return value.ToString().Length; } static int GetIntegerDigitCount(int valueInt) { // Version 2: use if-statements. 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; } }
1=1 1 100=3 3 -100=4 4
Method explanation. Here we explain how the GetIntegerDigitCount method is internally implemented. First, the int parameter is widened to a double value.
And This allows us to convert it to a positive value without losing information.
Then We test it against zero, and if it is negative, we set the sign variable to 1 and make it positive.
Finally We test against the constants (9, 99, 999) to determine its positive length. We return the sum of characters in the string.
Performance note. The ToString version will force an allocation on the managed heap and is likely much slower. But it is more robust.
ToString
A summary. We computed the number of digits in an integer using mathematical logic only. An if-statement can determine digit count. We also use the ToString method.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.