Search code examples
.netnumbersbase-class-library

Is there a "Number" struct/class in .NET?


I am attempting to store a variable length number that can have leading zeros as a part of that number.

Is there a class in the .NET framework capable of storing values like this without losing information about leading zeros, and could surpass the upper limit of a long?

I am currently storing them in a class like this, is there any way I could write this class better in the event there isn't some struct or class available in the BCL:

[Serializable]
public class Number
{
    public int[] Array { get; set; }
    public int Length { get { return Array.Length; } }

    public Number(string number)
    {
        Array = new int[number.Length];

        for (int i = 0; i < number.Length; i++)
        {
            Array[i] = Convert.ToInt32(number[i].ToString());
        }
    }

    public Number(int[] array)
    {
        Array = array;
    }

    public int ToInt()
    {
        return Convert.ToInt32(ToString());
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder(Array.Length);

        foreach (int i in Array)
            sb.Append(i);

        return sb.ToString();
    }
}

The ability to use this as a struct would be very useful, as would the ability to check equality easily.

Items in bold/italic are the requirements of such a class.


Solution

  • I'd suggest taking a look at this question about big integers in C#. You could expand them for the leading zeros issue.