I was wondering if there was a clean and short way to make a certain integer always to show with three digits, even if it is lower than 100 (i.e. 003, 005, 012, 057, 099). I do not like doing it like this:
if (i < 100)
{
if (i < 10)
{
someString = “00” + i.ToString();
}
else someString = “0” + i.ToString();
}
else
{
someString = i.ToString();
}
The ‘Dn’ parameter of ToString() specifies the number of leading 0’s; so for your example, requiring 3 leading zero’s, the code would be:
threeDigitString = i.ToString(“D3”);
See the reference here: http://msdn.microsoft.com/en-us/library/dd260048.aspx
i.ToString(“D3”) May not always work in Unity, but you have custom options that can be filled in as
int i = 23;
print("3 digit num = " + i.ToString("000") );
The same can be used to ensure padding for a minimum number of digits however many they might be.