private string PadZero(int source, int limit = 18)
{
StringBuilder sb = new StringBuilder(source.ToString().PadRight(limit, '0'));
if (source < 100000) sb.Insert(0, '0');
sb.Length = limit;
return sb.ToString();
}
Notes:
1. why use 'StringBuilder ? to reduce the creation of new strings, and keep memory use lower, as well as (probably) improve performance.
2. note the "lazy" strategy here: we pad the incoming number-turned-to-string with lots of zeroes, and we throw away the ones we don't need to return the result ... rather than calculate the required amount of padding.
I've tested this with all three of your input values, and get the expected result, but you should test this code further.
Do you need to handle negative numbers ? Do you an integer zero to be transformed to 18 char '0's ?