user1464139
user1464139

Reputation:

How can I convert the integer output of an enum to a two character string?

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

Answers (3)

Jeffom
Jeffom

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

Stephen Walker
Stephen Walker

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

Mark Byers
Mark Byers

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

Related Questions