Search code examples
c#numberspaddingserial-number

Ordered serial numbers with leading zeroes


I'm trying to generate a list of ordered serial numbers with leading zeroes like this:

00000000001
00000000002
00000000003
...
...
00000000101
00000000102
00000000103
...
...
00099999991
00099999992
...
...
99999999999 - END

My best attempt is this:

long fn;
for (fn = 100000000000; fn < 999999999999; fn++)
{
    Console.WriteLine(fn);
}

I want to write all these serial numbers to a file though I don't want it to look like it counted from 1 - 999999999999, but more like serial numbers generating from 000000000000 to 999999999999.


Solution

  • One option is to pad with zeroes:

    Console.WriteLine(fn.ToString().PadLeft(12, '0'));
    

    If fn = 123, this will print out 000000000123.