Question

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.

Was it helpful?

Solution 2

One option is to pad with zeroes:

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

If fn = 123, this will print out 000000000123.

OTHER TIPS

Grant's answer works, my preference would be:

Console.WriteLine("{0:00000000000}", fn);

or

Console.WriteLine(fn.ToString("00000000000"));

But really it is up to what is most readable to you...

See documentation on custom numeric string formats here: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top