将枚举转换为字符串数字

时间:2013-12-28 02:51:47

标签: c# enums

我想在C#中将enum转换为string三位数。

像这样:

enum test : short
{
    asdf, qwer, zxcv
}

test t = test.qwer;

string result = t.ToString("%03D") // Is this right?

我想打印001

1 个答案:

答案 0 :(得分:4)

在普通枚举中,您可以将其转换为整数,然后使用指定的格式调用“ToString”。在您的情况下,应该看起来像:

test t = test.qwer;
string result = ((int)t).ToString("000");

应该这样做!祝你好运。

相关问题