Saturday, 2 April 2016

formatting - C# convert int to string with padding zeros?




In C# I have an integer value which need to be convereted to string but it needs to add zeros before:



For Example:



int i = 1;


When I convert it to string it needs to become 0001



I need to know the syntax in C#.



Answer



i.ToString().PadLeft(4, '0') - okay, but doesn't work for negative numbers
i.ToString("0000"); - explicit form
i.ToString("D4"); - short form format specifier


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...