I'm working with numbers stored in a string in C#.
I have some number such as 22408061
. I would like to add dynamic 0 characters to have 12 characters giving 000022408061
.
As another example, given 12322408061
, it needs to be 012322408061
.
I'm working with numbers stored in a string in C#.
I have some number such as 22408061
. I would like to add dynamic 0 characters to have 12 characters giving 000022408061
.
As another example, given 12322408061
, it needs to be 012322408061
.
You are looking for PadLeft
method.
var str = "22408061".PadLeft(12, '0');
This is a string in c#.
You can use PadLeft
:
string text = "22408061";
text = text.PadLeft(12, '0'); // 000022408061
http://msdn.microsoft.com/en-us/library/92h5dc07(v=vs.110).aspx
Console.WriteLine(str.PadLeft(12, pad));
Logic is :
count the number of length, say for eg in 22408061 you will get 8 length
subtract 12 - 8 = 4 i.e. total value - length
which ever number you get, add that many zeros before the number.