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

发布于 2024-08-27 04:17:05 字数 443 浏览 6 评论 0原文

例如:

(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()

For example:

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

I really only need it for digits 1-9 in the case I'm working with, should I just use a switch/select case?

Update I won't need localization in this case either.

Update 2 Here's what I ended up using:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

上课铃就是安魂曲 2024-09-03 04:17:05

枚举怎么样?

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

编辑

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

How about an enumeration?

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,
}

Then you can type things like...

((Number)1).ToString()

If you need localization then you can add a DescriptionAttribute to each enum value. The attribute's Description property would store the name of the resourse item's key.

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...
}

The following function will grab the value of the Description property from the attribute

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;
}

This can be called in the following manner:

GetDescription(((Number)1))

From that you can then pull the relevant value from the resource file, or just call .ToString() if null was returned.

Edit

Various commenters have pointed out (and I have to agree) that it would be simpler to just use the enum value names to reference localised strings.

御守 2024-09-03 04:17:05

创建字符串字典:

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

string word = digits[digit];

create a dictionary of strings:

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

string word = digits[digit];
儭儭莪哋寶赑 2024-09-03 04:17:05

使用查找表;一个数组就可以了。它并不比枚举慢,而且更容易本地化。

编辑

Andrey 的代码示例就是我所建议的,尽管我认为称其为字典有点令人困惑。

Use a lookup table; an array will do. It's no slower than an enum, and it's easier to localize.

edit

Andrey's code sample is what I was suggesting, although I think calling it a dictionary is a bit confusing.

洋洋洒洒 2024-09-03 04:17:05

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

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

If you don't need localization, I'd suggest Richard Ev's solution. For localization, however, I'd suggest adding the ten digit names to a resource file, for example NumberName_0 to NumberName_9. This way, when looking up a number, you can just load the resource with the name String.Format("NumberName_{0}", mydigit).

The same technique, by the way, also works fine for localizable enumeration names or descriptions.

荒路情人 2024-09-03 04:17:05

为什么停在 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;
    }

Why stop at 1-9...

C#'s version of the way Squeak Smalltalk does it for all numbers to a vigintillion:

    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;
    }
我不吻晚风 2024-09-03 04:17:05

我认为没有任何内置函数可以做到这一点。我会使用选择案例。

I don't think there are any built-in functions to do this. I would use select case.

意中人 2024-09-03 04:17:05

如果您使用的是 2008:

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

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

If you are using 2008:

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

enum Number {
    One = 1,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文