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

ToArray

Returns an array of int containing all caracters that compose the number.

Source

public static int[] ToArray(this int number)
{
    if (number == 0)
    {
        return new int[0];
    }
    else if (number < 0)
    {
        number = -1 * number;
    }

    List<int> list = new List<int>();
    while (number > 0)
    {
        list.Add(number % 10);
        number = number / 10;
    }
    list.Reverse();

    return list.ToArray();
}

Example

int num = 87559;
int[] arr = num.ToArray();

Author: waldyrfelix

Submitted on: 11 aug. 2009

Language: C#

Type: System.Int32

Views: 5424