0

For class I have an assignment that wants me to translate numerical values to the word equivalants. Up too 9999.

I achieved doing this with the dictionary command, but am unable to do so after setting up an Array. We're only our fourth week of class so its difficult for me.

Here are my arrays

string[] ToWordsOne = new string[10] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string[] ToWordsTen = new string[8] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
string[] ToWordsTeens = new string[9] { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

The program seems to recognize this just fine, but when I go to output the code it doesn't say anything or crashes (depending on the changes I make.)

 int i = 0;
        string output = "";
        while (i <= input.Length)
        {
            if (i == (input.Length))
            {
                output = output + " " + ToWordsOne[i];
            }
            if (i == (input.Length))
            {
                if (input == 1)
                {
                    output = ToWordsTeens[i];
                }

                else
                {
                    output = output + ToWordsTen[i];
                }

            }
            if (i == (input.Length))
            {
                output = output + ToWordsOne[i] + " hundred" + " ";
            }
            if (i == (input.Length))
            {
                output = ToWordsOne[i] + " thousand" + " ";
            }
            i++;

This is the code I used for outputting the results from dictionary. I am aware that I can't use the ".length" method. But I'm unaware of where to go from there.

  • there is a very good example here. http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words – active92 Sep 15 '16 at 04:45
  • 2
    Possible duplicate of [converting numbers in to words C#](http://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp) – JeetDaloneboy Sep 15 '16 at 04:55
  • Possible duplicate - [How can I convert an integer into its verbal representation?](http://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation). – ram Sep 15 '16 at 05:04

1 Answers1

1

as per this post: converting-numbers-in-to-words-c-sharp you can convert a number to word like below.

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;
    }
Community
  • 1
  • 1
JeetDaloneboy
  • 399
  • 2
  • 16