An integer doesn't have leading zeros. Integers are numeric values, they don't carry display information with them. So if you're storing the value as an integer then you're not storing any display information. In that case you apply the display information when you display the value:
int a = 10;
Console.WriteLine(Convert.ToString(a).PadLeft(5, '0'));
If you want the display information to be retained as part of the value itself, make it a string:
string a = "00010";
Console.WriteLine(a);
If it needs to be stored as an integer and you don't want to re-write the display logic many times, you can encapsulate that logic into a custom type which wraps the integer:
public class PaddedInteger
{
private int Value { get; set; }
private int PaddingSize { get; set; }
private char PaddingCharacter { get; set; }
public PaddedInteger(int value, int paddingSize, char paddingCharacter)
{
Value = value;
PaddingSize = paddingSize;
PaddingCharacter = paddingCharacter;
}
public override string ToString()
{
return Convert.ToString(Value).PadLeft(PaddingSize, PaddingCharacter);
}
}
Then in your code:
PaddedInteger a = new PaddedInteger(10, 5, '0');
Console.WriteLine(a);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…