Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - increment number and keep zeros in front

Tags:

c#

int

increment

I need to make a 40 digit counter variable. It should begin as 0000000000000000000000000000000000000001
and increment to
0000000000000000000000000000000000000002

When I use the int class, it cuts off all the zeros. Problem is I need to increment the number and then convert it to a string, with the correct number of leading zeros. The total size should be 40 digits. So if I hit 50 for example, it should look like this:

0000000000000000000000000000000000000050

How can I do that and retain the zeros?

like image 713
blizz Avatar asked Jun 07 '12 15:06

blizz


1 Answers

Use the integer and format or pad the result when you convert to a string. Such as

int i = 1;
string s = i.ToString().PadLeft(40, '0');

See Jeppe Stig Nielson's answer for a formatting option that I can also never remember.

like image 174
Anthony Pegram Avatar answered Sep 28 '22 05:09

Anthony Pegram