简体   繁体   中英

Datatypes in c#

I am in need of an integer data type which starts with 00 from 1 to 10 and 0 from 10 to 99.

Is there any data type in c#

Thank you very much....

You can use pretty much any integer type for that( int , uint , etc); the important part is how you format it. In this case:

string s = i.ToString("000");

The integer data type is just the value - leading zeros don't exist or not exist - simply that isn't the job of an integer.

You could always create a custom struct of course, taking the integer value in the constuctor (and maybe a custom conversion operator), overriding the ToString() method (and probably Equals , GetHashCode , etc).


Just for kicks:

public struct TriDigit : IComparable, IComparable<TriDigit>, IComparable<int>, IEquatable<TriDigit>, IEquatable<int>
{
    private readonly int value;
    public TriDigit(int value)
    {
        if (value < 0 || value > 999) throw new ArgumentOutOfRangeException("value");
        this.value = value;
    }
    public override string ToString()
    {
        return value.ToString("000");
    }
    public override bool Equals(object obj)
    {
        if (obj == null) return false;
        if (obj is TriDigit) return ((TriDigit)obj).value == value;
        if (obj is int) return ((int)obj) == value;
        return false;
    }
    public int CompareTo(object obj)
    {
        if (obj == null) return -1;
        if(obj is TriDigit) return value.CompareTo(((TriDigit)obj).value);
        if (obj is int) return value.CompareTo((int)obj);
        return -1;
    }
    public override int GetHashCode()
    {
        return value;
    }
    public static explicit operator TriDigit(int value)
    {
        return new TriDigit(value);
    }
    public static implicit operator int(TriDigit value)
    {
        return value.value;
    }

    int IComparable<TriDigit>.CompareTo(TriDigit other)
    {
        return value.CompareTo(other.value);
    }
    int IComparable<int>.CompareTo(int other)
    {
        return value.CompareTo(other);
    }
    bool IEquatable<TriDigit>.Equals(TriDigit other)
    {
        return value == other.value;
    }
    bool IEquatable<int>.Equals(int other)
    {
        return value == other;
    }
}

听起来您需要格式字符串,而不是数据类型。

Console.WriteLine("{0:D3}", i);

You can simply use int or if it must not be negative uint (unsigned integer). There are smaller data types, but for most applications they're not worth worrying about.

If you are saying that you need an integer with a legal range from 1 to 10, with an index that starts at zero, then the answer is no. You'll need to make your own an ADT for that.

It's formatting you want, not a new datatype. Read all about it here: MSDN's page on Custom Numeric Format Strings .

You can use PadLeft(int totalWidth, char paddingChar) like..

int myNumber=12
string myStringNumber = myNumber.ToString().PadLeft(3, '0');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM