How can I get a count of the total number of digits in a number?

Arash picture Arash · Dec 19, 2010 · Viewed 158.6k times · Source

How can I get a count of the total number of digits of a number in C#? For example, the number 887979789 has 9 digits.

Answer

Steve picture Steve · Dec 19, 2010

Without converting to a string you could try:

Math.Ceiling(Math.Log10(n));

Correction following ysap's comment:

Math.Floor(Math.Log10(n) + 1);