i want to make a function that counts the amount of digits once the value is sumed up
lets say i have this array
byte[] array = new byte[] { 200, 100, 200, 250, 150, 100, 200 };
once this is sumed up you’ll have a value of 1200
you can get the amount of digits with these functions
Math.Floor(Math.Log10(1200)+1) // 4
but if i sum it up and there are too many values in the array i get an integer overflow
public decimal countDigits(byte[] array)
{
decimal count = array[0];
for (int i = 1; i < array.Length; i++)
{
count = Math.Log10(Math.Pow(count, 10)+array[i]);
}
return count;
}
this does give the correct output i want but this causes a integeroverflow if the count is greater than 28.898879583742193 (log10(decimal.MaxValue))
>Solution :
There’s a BigInteger data type that can sum up arbitrary (integer) values. It even has a Log10 method, so it works very similar to a standard integer variable. The only limitation is that the result of BigInteger.Log10 must be smaller than Double.MaxValue, but that sounds like a reasonable limitation. (10^1E308=10^10^308 is a really huge number)