TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Integer Digit Count

This C# article shows how to count the number of digits in an int variable or parameter.

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

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

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.

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


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf