我正在尝试制作一个将数字转换为单词的程序,我知道它存在并由Converting numbers in to words回答

但是我想做的是例如我有一个输入114321程序进入循环,首先处理321three hundred twenty one然后是114,但是前缀为千个one hundred fourteen thousand,所以总体上输出类似于one hundred fourteen thousand three hundred twenty one。我这样尝试过,但它的错误和即时消息甚至没有关闭

public static string NumberToWords(int number)
    {
        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" };
        var hundredsMap = new[] { "", "Thousand", "Million" };
        string words = "";
        string snumber = number.ToString();
        int tempInt = number;
        int counter;

        for (counter = 0;counter <= (snumber.Length/3); counter++)
        {
            if (snumber.Length > 3)
            {
                tempInt = Convert.ToInt32(snumber.Substring(snumber.Length - 3));
            }

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

            if (tempInt != 0)
            {
                if (tempInt < 20)
                {
                    words += unitsMap[tempInt];
                }
                else
                {
                    words += tensMap[tempInt / 10];
                    if ((tempInt % 10) > 0)
                    {
                        words += " " + unitsMap[tempInt % 10];
                    }
                }
            }
            words += hundredsMap[counter];
        }
        return words;
    }

最佳答案

以最小的更改来修复代码的主要技巧是,您应该以从右到左的顺序分别为每个组构建字符串,然后以从左到右的顺序将它们添加到字符串中。这样,您还可以相对轻松地解决对其他空格的需求(我的意思是,当您拥有“ 1000300”时,尽管整个“千”组都丢失了,但所有单词之间只有一个空格,而您会得到“ one_million_three_hundreds”)。

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

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

        string fullString = "";
        bool negative = (number < 0);
        int rest = Math.Abs(number);


        for (int counter = 0; rest > 0; counter++)
        {
            string groupWords = "";
            int currentGroup = rest % 1000;
            rest /= 1000;

            int hundreds = currentGroup / 100;
            if (hundreds > 0)
            {
                groupWords += unitsMap[hundreds] + " hundred";
                currentGroup %= 100;
            }

            if (currentGroup != 0)
            {
                if (groupWords.Length > 0)
                    groupWords += " ";
                if (currentGroup < 20)
                {
                    groupWords += unitsMap[currentGroup];
                }
                else
                {
                    groupWords += tensMap[currentGroup / 10];
                    if ((currentGroup % 10) > 0)
                    {
                        groupWords += " " + unitsMap[currentGroup % 10];
                    }
                }
            }

            // handle case such as just "one million"
            if (groupWords.Length > 0)
            {
                groupWords += hundredsMap[counter];
                // handle case such as just "one million"
                if (fullString.Length == 0)
                    fullString = groupWords;
                else
                    fullString = groupWords + " " + fullString;
            }
        }
        return negative ? "minus " + fullString : fullString;
    }


还要注意,您根本不需要snumber(更不用说使用中的错误了,您每个组总是得到最后3位数字)

关于c# - 单词的新数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43748700/

10-12 00:31