bob
bob

Reputation: 7

Padding a number with leading 0 in C#

Hi i have this int (example starts with 10)and i have an incremental function which increments the int so the next number would be 11. I would like to change the int to 010 and 011 etc etc. I found this code online which helps to pad the number but i am not sure how to display put it to a variable...

int intValue = 10;
Console.WriteLine("{0,22} {1,22}", intValue.ToString("D3"), intValue.ToString("X3"));
Console.WriteLine("{0,22:D8} {0,22:X8}", byteValue);

i would like to change it so that i can create a new intvalue2(which is not created yet) to be 010. Thanks in advance...

Upvotes: 0

Views: 279

Answers (1)

Nico
Nico

Reputation: 12683

The .ToString() method is creating a string not another int. So the number 010 is not actually a number but a string.

string pInt = intValue.ToString("D3");

Now pInt = 010 represented as a string.

Upvotes: 1

Related Questions