我必须写一个数字的级数,有(每个)5位数字。我的代码是:
int count = 1;
string labelCount = "";
foreach (var directory in folderList)
{
if (count < 10)
{
labelCount = "0000" + count.ToString();
}
else if (count < 100)
{
labelCount = "000" + count.ToString();
}
else if (count < 1000)
{
labelCount = "00" + count.ToString();
}
else if (count < 10000)
{
labelCount = "0" + count.ToString();
}
count++;
}
但在我看来,它看起来不太好。有没有办法格式化一个数字(在左边添加 0xN)或者这是唯一的方法?