Hi All,
We commonly come across situations where we need to pad a number with leading zeroes.
The simplest way of doing it is using string.Format() and {0:d3}
For example I have an integer i containing value 2 but I need 002, in this case I would use
string strNew = string.Format("{0:d3}", i);
Here 3 is total field width. If i need 00002, I would go for d5
Now this problem is solved, here is another problem: what I don't know in advance the size of the field or in other words, what if the field width is a variable.
Here is the solution:
string strFormat = "{0:d" + iFieldWIdth.ToString() + "}";
string strPaddedValue = string.Format(strFormat, i);
Enjoy
No comments:
Post a Comment