12

如标题所示,有谁知道 .NET 或第 3 方库中是否有一个地方可以将整数转换为“排序”对应物。

1 - first
2 - second
3 - third
etc...

我当然可以自己写一个,但如果可能的话,我宁愿重用已经存在的东西。

谢谢。

4

3 回答 3

13

Humanizer 是一个很棒的库:Humanizr

Install-Package Humanizer

使用 Humanizer,您可以:

int number = 5;
string ordinal = number.ToOrdinalWords()
于 2014-07-07T17:19:29.750 回答
1

将数字转换为单词 C#

类似于这个问题。你可以得到你需要的LukeH's answer

public static string NumberToWords(int number)
{
if (number == 0)
    return "zero";

if (number < 0)
    return "minus " + NumberToWords(Math.Abs(number));

string words = "";

if ((number / 1000000) > 0)
{
    words += NumberToWords(number / 1000000) + " million ";
    number %= 1000000;
}

if ((number / 1000) > 0)
{
    words += NumberToWords(number / 1000) + " thousand ";
    number %= 1000;
}

if ((number / 100) > 0)
{
    words += NumberToWords(number / 100) + " hundred ";
    number %= 100;
}

if (number > 0)
{
    if (words != "")
        words += "and ";

    var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

    if (number < 20)
        words += unitsMap[number];
    else
    {
        words += tensMap[number / 10];
        if ((number % 10) > 0)
            words += "-" + unitsMap[number % 10];
    }
}

return words;
}
于 2012-09-18T14:45:16.793 回答
0

我不知道有一个预先存在的库来处理这个问题,但这里有一个非常简单的方法: .NET 将数字转换为字符串表示(1 到 1、2 到 2 等……)

于 2012-09-18T14:46:37.780 回答