ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Length

Determines how many numbers compose the integer if it was represented as a string.

Source

public static int Length(this int i)
{
    if (i <= 0) throw new ArgumentOutOfRangeException();

    return (int)Math.Floor(Math.Log10(i)) + 1;
}

Example

int ir = 84372;

Console.WriteLine(ir.Length()); //will write 5

Author: Phil Campbell

Submitted on: 24 mrt. 2010

Language: C#

Type: System.Int32

Views: 5211