Formatting will not result in an error if there are too many digits. You can get a 6-digit string with left padding only with
string output = number.ToString("000000");
If you need 7-digit strings that will be invalid, you just need to code it.
if (number >= 0 and number < 1000000) { output = number.ToString("000000") } else { throw new ArgumentException("number"); }
To use string.Format you must write
string output = string.Format("{0:000000}", number);
Anthony pegram
source share