单词的新数字

时间:2017-05-02 23:19:44

标签: c#

我正在努力制作一个程序,将数字改为单词,我知道它存在并由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;
    }

2 个答案:

答案 0 :(得分:1)

使用最少的更改修复代码的主要技巧是,您应该按从右到左的顺序分别为每个组构建字符串,然后按从左到右的顺序将它们添加到字符串中。通过这种方式,您还可以相对轻松地处理额外空间的需求(我的意思是当您拥有“1000300”时,您会得到“one_million_three_hundreds”,所有单词之间只有一个空格,尽管整个“数千”组都丢失了。)< / p>

    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位数字)

答案 1 :(得分:1)

您可以使用以下控制台应用程序代码作为参考来获得结果。 代码将接受最多2位小数的货币值,并以英文打印。

namespace ConsoleApplication2
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    class Program
    {
       static void Main(string[] args)
        {
            bool repeat = true;
            while (repeat)
            {
                string inputMonetaryValueInNumberic = string.Empty;
                string centPart = string.Empty;
                string dollarPart = string.Empty;
                Console.Write("\nEnter the monetary value : ");
                inputMonetaryValueInNumberic = Console.ReadLine();
                inputMonetaryValueInNumberic = inputMonetaryValueInNumberic.TrimStart('0');

                if (ValidateInput(inputMonetaryValueInNumberic))
                {

                    if (inputMonetaryValueInNumberic.Contains('.'))
                    {
                        centPart = ProcessCents(inputMonetaryValueInNumberic.Substring(inputMonetaryValueInNumberic.IndexOf(".") + 1));
                        dollarPart = ProcessDollar(inputMonetaryValueInNumberic.Substring(0, inputMonetaryValueInNumberic.IndexOf(".")));
                    }
                    else
                    {
                        dollarPart = ProcessDollar(inputMonetaryValueInNumberic);
                    }
                    centPart = string.IsNullOrWhiteSpace(centPart) ? string.Empty : " and " + centPart;
                    Console.WriteLine(string.Format("\n\n{0}{1}", dollarPart, centPart));
                }
                else
                {
                    Console.WriteLine("Invalid Input..");
                }

                Console.WriteLine("\n\nPress any key to continue or Escape of close : ");
                var loop = Console.ReadKey();
                repeat = !loop.Key.ToString().Contains("Escape");
                Console.Clear();
            }

        }

        private static string ProcessCents(string cents)
        {
            string english = string.Empty;
            string dig3 = Process3Digit(cents);
            if (!string.IsNullOrWhiteSpace(dig3))
            {
                dig3 = string.Format("{0} {1}", dig3, GetSections(0));
            }
            english = dig3 + english;
            return english;
        }
        private static string ProcessDollar(string dollar)
        {
            string english = string.Empty;
            foreach (var item in Get3DigitList(dollar))
            {
                string dig3 = Process3Digit(item.Value);
                if (!string.IsNullOrWhiteSpace(dig3))
                {
                    dig3 = string.Format("{0} {1}", dig3, GetSections(item.Key));
                }
                english = dig3 + english;
            }
            return english;
        }
        private static string Process3Digit(string digit3)
        {
            string result = string.Empty;
            if (Convert.ToInt32(digit3) != 0)
            {
                int place = 0;
                Stack<string> monetaryValue = new Stack<string>();
                for (int i = digit3.Length - 1; i >= 0; i--)
                {
                    place += 1;
                    string stringValue = string.Empty;
                    switch (place)
                    {
                        case 1:
                            stringValue = GetOnes(digit3[i].ToString());
                            break;
                        case 2:
                            int tens = Convert.ToInt32(digit3[i]);
                            if (tens == 1)
                            {
                                if (monetaryValue.Count > 0)
                                {
                                    monetaryValue.Pop();
                                }
                                stringValue = GetTens((digit3[i].ToString() + digit3[i + 1].ToString()));
                            }
                            else
                            {
                                stringValue = GetTens(digit3[i].ToString());
                            }
                            break;
                        case 3:
                            stringValue = GetOnes(digit3[i].ToString());
                            if (!string.IsNullOrWhiteSpace(stringValue))
                            {
                                string postFixWith = " Hundred";
                                if (monetaryValue.Count > 0)
                                {
                                    postFixWith = postFixWith + " And";
                                }
                                stringValue += postFixWith;
                            }
                            break;
                    }
                    if (!string.IsNullOrWhiteSpace(stringValue))
                        monetaryValue.Push(stringValue);
                }
                while (monetaryValue.Count > 0)
                {
                    result += " " + monetaryValue.Pop().ToString().Trim();
                }
            }
            return result;
        }
        private static Dictionary<int, string> Get3DigitList(string monetaryValueInNumberic)
        {
            Dictionary<int, string> hundredsStack = new Dictionary<int, string>();
            int counter = 0;
            while (monetaryValueInNumberic.Length >= 3)
            {
                string digit3 = monetaryValueInNumberic.Substring(monetaryValueInNumberic.Length - 3, 3);
                monetaryValueInNumberic = monetaryValueInNumberic.Substring(0, monetaryValueInNumberic.Length - 3);
                hundredsStack.Add(++counter, digit3);
            }
            if (monetaryValueInNumberic.Length != 0)
                hundredsStack.Add(++counter, monetaryValueInNumberic);
            return hundredsStack;
        }
        private static string GetTens(string tensPlaceValue)
        {
            string englishEquvalent = string.Empty;
            int value = Convert.ToInt32(tensPlaceValue);
            Dictionary<int, string> tens = new Dictionary<int, string>();
            tens.Add(2, "Twenty");
            tens.Add(3, "Thirty");
            tens.Add(4, "Forty");
            tens.Add(5, "Fifty");
            tens.Add(6, "Sixty");
            tens.Add(7, "Seventy");
            tens.Add(8, "Eighty");
            tens.Add(9, "Ninty");
            tens.Add(10, "Ten");
            tens.Add(11, "Eleven");
            tens.Add(12, "Twelve");
            tens.Add(13, "Thrteen");
            tens.Add(14, "Fourteen");
            tens.Add(15, "Fifteen");
            tens.Add(16, "Sixteen");
            tens.Add(17, "Seventeen");
            tens.Add(18, "Eighteen");
            tens.Add(19, "Ninteen");
            if (tens.ContainsKey(value))
            {
                englishEquvalent = tens[value];
            }

            return englishEquvalent;

        }
        private static string GetOnes(string onesPlaceValue)
        {
            int value = Convert.ToInt32(onesPlaceValue);
            string englishEquvalent = string.Empty;
            Dictionary<int, string> ones = new Dictionary<int, string>();
            ones.Add(1, " One");
            ones.Add(2, " Two");
            ones.Add(3, " Three");
            ones.Add(4, " Four");
            ones.Add(5, " Five");
            ones.Add(6, " Six");
            ones.Add(7, " Seven");
            ones.Add(8, " Eight");
            ones.Add(9, " Nine");

            if (ones.ContainsKey(value))
            {
                englishEquvalent = ones[value];
            }

            return englishEquvalent;
        }
        private static string GetSections(int section)
        {
            string sectionName = string.Empty;
            switch (section)
            {
                case 0:
                    sectionName = "Cents";
                    break;
                case 1:
                    sectionName = "Dollars";
                    break;
                case 2:
                    sectionName = "Thousand";
                    break;
                case 3:
                    sectionName = "Million";
                    break;
                case 4:
                    sectionName = "Billion";
                    break;
                case 5:
                    sectionName = "Trillion";
                    break;
                case 6:
                    sectionName = "Zillion";
                    break;
            }
            return sectionName;
        }
        private static bool ValidateInput(string input)
        {
            return Regex.IsMatch(input, "[0-9]{1,18}(\\.[0-9]{1,2})?"))
        }
    }
}