Count Number of digits in a number in C#
To count the number of digits in a number we can useMath.Floor()
method and pass Math.Log10() + 1
as parameter, which returns the base 10 logarithm of a specified number + 1.Example
using System;
public class Program
{
public static void Main() {
int wholeValue = 9856;
double numberOfDigits = Math.Floor(Math.Log10(wholeValue) + 1);
Console.WriteLine(numberOfDigits);
}
}
Output
4