简体   繁体   English

C#.net中的长序字符串的整数

[英]Integer to long Ordinal string in c#.net

Does anyone knows how to convert an integer to a long ordinal word in C# ? 有谁知道如何在C#中将整数转换为长序单词?

Is easy to convert 1 to 1st, 2 to 2nd using this algorithm described here: Is there an easy way to create ordinals in C#? 使用此处描述的算法很容易将1转换为1st,将2转换为2nd: 是否有一种简单的方法可以在C#中创建序数? . But i need a way to convert 1 to the long ordinal word version. 但是我需要一种将1转换为长序单词版本的方法。 For example: 例如:

1 => First 1 =>首先

2 => Second 2 =>秒

3 => Third ... 3 =>第三...

9 => Ninth and so on for any number. 9 =>第九,依此类推。

The solution can not either create an infinite dictionary list with key par values of (1, "first", 2, "second", etc, etc) 该解决方案不能创建键值分别为(1,“ first”,2,“ second”等的无限字典列表)

I too cant find the exact solution for that.But I managed to get the answer.Try this,It will be useful. 我也找不到确切的解决方案。但是我设法得到了答案。尝试一下,它将很有用。

public string ConvertNumbertoWordsFirst(long number)
    {
        string words = "";
        words = ConvertNumbertoWords(number);
        words = words.TrimEnd('\\');
        if (words.EndsWith("One"))
        {
            words = words.Remove(words.LastIndexOf("One") + 0).Trim();
            words = words + "First";
        }
        else if (words.EndsWith("Two"))
        {
            words = words.Remove(words.LastIndexOf("Two") + 0).Trim();
            words = words + "Second";
        }
        else if (words.EndsWith("Three"))
        {
            words = words.Remove(words.LastIndexOf("Three") + 0).Trim();
            words = words + "Third";
        }
        else if (words.EndsWith("Five"))
        {
            words = words.Remove(words.LastIndexOf("Five") + 0).Trim();
            words = words + "Fifth";
        }
        else if (words.EndsWith("Eight"))
        {
            words = words.Remove(words.LastIndexOf("Eight") + 0).Trim();
            words = words + "Eighth";
        }
        else if (words.EndsWith("Nine"))
        {
            words = words.Remove(words.LastIndexOf("Nine") + 0).Trim();
            words = words + "Ninth";
        }
        else
        {
            words = words + "th";
        }
        return words;
    }

ConvertNumbertoWords() is the function i got it from the below link. ConvertNumbertoWords()是我从下面的链接获得的功能。 Link : converting numbers in to words C# 链接: 将数字转换为单词C#

public string ConvertNumbertoWords(long number)
    {
        if (number == 0) return "Zero";
        if (number < 0) return "minus " + ConvertNumbertoWords(Math.Abs(number));
        string words = "";
        if ((number / 100000) > 0)
        {
            words += ConvertNumbertoWords(number / 100000) + " Lakhs ";
            number %= 100000;
        }
        if ((number / 1000) > 0)
        {
            words += ConvertNumbertoWords(number / 1000) + " Thousand ";
            number %= 1000;
        }
        if ((number / 100) > 0)
        {
            words += ConvertNumbertoWords(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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM