在.Net中是否有办法获取int的单词的字符串值?

时间:2010-03-29 16:30:13

标签: .net vb.net string integer int

例如:

(1).SomeFunction().Equals("one")
(2).SomeFunction().Equals("two")

在我正在使用的情况下,我真的只需要数字1-9,我应该只使用一个开关/选择案例吗?

更新在这种情况下我也不需要本地化。

更新2 以下是我最终使用的内容:

Private Enum EnglishDigit As Integer
    zero
    one
    two
    three
    four
    five
    six
    seven
    eight
    nine
End Enum

(CType(someIntThatIsLessThanTen, EnglishDigit)).ToString()

7 个答案:

答案 0 :(得分:11)

枚举怎么样?

enum Number
{
    One = 1, // default value for first enum element is 0, so we set = 1 here
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
}

然后你可以输入像......这样的东西。

((Number)1).ToString()

如果您需要本地化,则可以为每个枚举值添加DescriptionAttribute。属性的Description属性将存储资源项的密钥名称。

enum Number
{
    [Description("NumberName_1")]
    One = 1, // default value for first enum element is 0, so we set = 1 here 

    [Description("NumberName_2")]
    Two,

    // and so on...
}

以下函数将从属性

中获取Description属性的值
public static string GetDescription(object value)
{
    DescriptionAttribute[] attributes = null;
    System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString());
    if (fi != null)
    {
        attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
    }

    string description = null;
    if ((attributes != null) && (attributes.Length > 0))
    {
        description = attributes[0].Description;
    }

    return description;
}

可以通过以下方式调用:

GetDescription(((Number)1))

然后,您可以从资源文件中提取相关值,或者只要在返回.ToString()时调用null

修改

各种评论者指出(我必须同意)只使用枚举值名称来引用本地化字符串会更简单。

答案 1 :(得分:4)

创建字符串字典:

string[] digits = new string[] 
{
   "zero",
   "one",
   "two",
   ...
};

string word = digits[digit];

答案 2 :(得分:1)

使用查找表;一个数组会做。它不比枚举慢,并且更容易本地化。

修改

安德烈的代码示例是我的建议,虽然我认为把它称为字典有点令人困惑。

答案 3 :(得分:1)

如果您不需要本地化,我建议使用Richard Ev的解决方案。但是,对于本地化,我建议将十位数名称添加到资源文件中,例如NumberName_0NumberName_9。这样,在查找数字时,您只需加载名为String.Format("NumberName_{0}", mydigit)的资源。

顺便说一句,相同的技术也适用于可本地化的枚举名称或描述。

答案 4 :(得分:1)

为什么要停在1-9 ......

C#的版本Squeak Smalltalk为所有数字做了一个警告:

    public static String AsWords(this int aNumber) {
        var answer = "";
        if (aNumber == 0) return "zero";
        if (aNumber < 0) {
            answer = "negative";
            aNumber = Math.Abs(aNumber);
        }

        var thousands = new[] {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion","octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion"};
        var thousandCount = 0;
        while (aNumber > 0) {
            var underOneThousandName = ThreeDigitName(aNumber % 1000);
            aNumber = aNumber / 1000;
            if(underOneThousandName != "") {
                if (answer != "") answer = "," + answer;
                answer = underOneThousandName + " " + thousands[thousandCount] + answer;
            }
            thousandCount += 1;
        }
        return answer;
    }

    private static string ThreeDigitName(int aNumberLessThanOneThousand) {
        if (aNumberLessThanOneThousand == 0) return "";
        var units = new[] {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen"};
        var answer = "";
        if (aNumberLessThanOneThousand > 99) {
            answer = units[(aNumberLessThanOneThousand / 100) - 1] + " hundred";
            if (aNumberLessThanOneThousand % 100 != 0)
                answer += " " + ThreeDigitName(aNumberLessThanOneThousand % 100);
            return answer;
        }
        if (aNumberLessThanOneThousand < 20) return units[aNumberLessThanOneThousand -1];
        var multiplesOfTen = new[] {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        answer += multiplesOfTen[(aNumberLessThanOneThousand / 10)-2];
        if (aNumberLessThanOneThousand % 10 != 0) answer += "-" + units[(aNumberLessThanOneThousand % 10)-1];
        return answer;
    }

答案 5 :(得分:0)

我认为没有任何内置函数可以做到这一点。我会用select case。

答案 6 :(得分:0)

如果您使用的是2008:

public static String AsWord(this int aNumber) {
    return ((Number) aNumber).ToString();
}

enum Number {
    One = 1,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
}