Reputation:
For the following Enum:
public enum ContentKey {
Menu = 0,
Article = 1,
FavoritesList = 2
};
The enum ContentKey returns an integer 0,1,2. How can I convert or cast this so that it returns a two digit zero padded string "00", "01" .. "99" etc
Upvotes: 2
Views: 674
Reputation: 542
String.Format("{0:00}", (int)<youEnum>),
and for enum you only need to do this
public enum ContentKey {
Menu = 0,
Article,
FavoritesList
};
it will automaticly set the the values for you
Upvotes: 5
Reputation: 574
I don't believe you can pad an int with 0s.
If you want to do it with a string however, you can use:
string text = value.ToString("00");
Upvotes: 1
Reputation: 838716
When you call ToString
you can use the format string "00"
to ensure that you get at least two digits:
string result = ((int)contentKey).ToString("00");
Upvotes: 11