The obvious, quick and easy solution to get the length of a number as a string would be this.

var number = 1234;
var digitCount = number.ToString().Length; // output: 4

There is nothing wrong with this. There is just a more performance efficient solution to this and it is called ISpanFormattable.

All integral numeric types (int, long, etc.) and floating-point numeric types (double, decimal, etc.) in .NET since version 6 implement the ISpanFormattable interface which relies on System.Span<T> and System.ReadOnlySpan<T>.

By using the ISpanFormattable.TryFormat method we avoid the allocation on the heap. More on Stack vs. Heap memory.

// Run code on sharplab.io
var number = 1234;
Span<char> digits = stackalloc char[10]; // int.MaxValue.ToString().Length == 10
number.TryFormat(digits, out var charsWritten);
var digitCount = digits[..charsWritten].Length;