Check the length of integer variable

c#

Solution

The following worked a treat for me!

public static int IntLength(int i)
{
  if (i < 0)
    throw new ArgumentOutOfRangeException();
  if (i == 0)
    return 1;
  return (int)Math.Floor(Math.Log10(i)) + 1;
}

Original Source: http://www.java2s.com/Code/CSharp/Data-Types/Getthedigitlengthofanintvalue.htm

Problem

Is there a way to check a lenth of integer variable, and if is to long just trim it. I hava a field in database that accepts 3 character, lenth is 3. So is it possible to do like it's done with string variable example: ``` cust_ref = cust_ref.Length > 20 ? cust_ref.Substring(0, 19) : cust_ref; ``` Thanks!

Original source