I have to write a progression of number, having (each) 5 digit. My code is:
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++;
}
but it doesnt looks so good in my opinion. Is there a way to format a number (adding 0xN to the left) or that's the only way?
Just give format to ToString method
var str = count.ToString("00000");
Take a look at String.PadLeft:
string formatted = count.ToString().PadLeft(6, '0');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With