Jump to content


Photo
* * * * * 1 votes

Convert Number to Words C# Console Application

C# Console Application

  • Please log in to reply
4 replies to this topic

#1 EC_Joe

EC_Joe

    Root Administrator

  • EC-Owner
  • PipPipPipPipPipPipPip
  • 253 posts

Posted 17 April 2010 - 09:16 AM

This is another program I ended up writing for a homework assignment (though I went far beyond the requirements). I learned how to easily convert numbers of integer type to words. It was slightly more difficult to allow doubles to be converted but I was able to be successful in that implementation as well. The following script allows a user to specify a double down to 7 decimal places (ten-millionths) and convert that number into a string of words/amount.

Edit: Originally this only supported the millions place. Now it supports very large numbers when you do not specify a decimal point. I am still working out some kinks to try and make the program support very large decimal numbers when a decimal point is used. Any constructive criticism welcome :)

Source Code for Console Application C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumWords
{
    class Program
    {
        // PROGRAM HANDLES NEGATIVE AND POSITIVE DOUBLES


        static String NumWordsWrapper(double n)
        {
            string words = "";
            double intPart;
            double decPart = 0;
            if (n == 0)
                return "zero";
            try
            {
                string[] splitter = n.ToString().Split('.');
                intPart = double.Parse(splitter[0]);
                decPart = double.Parse(splitter[1]);
            }
            catch
            {
                intPart = n;
            }

            words = NumWords(intPart);

            if (decPart > 0)   
            {
                if (words != "")
                    words += " and ";
                int counter = decPart.ToString().Length;
                switch(counter)
                {
                    case 1: words += NumWords(decPart) + " tenths"; break;
                    case 2: words += NumWords(decPart) + " hundredths"; break;
                    case 3: words += NumWords(decPart) + " thousandths"; break;
                    case 4: words += NumWords(decPart) + " ten-thousandths"; break;
                    case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
                    case 6: words += NumWords(decPart) + " millionths"; break;
                    case 7: words += NumWords(decPart) + " ten-millionths"; break;
                }
            }
        return words;
    }

        static String NumWords(double n) //converts double to words
        {
            string[] numbersArr = new string[]{"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
            string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
            string[] suffixesArr = new string[] {"thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","Quattuordecillion","Quindecillion","Sexdecillion","Septdecillion","Octodecillion","Novemdecillion","Vigintillion" };
            string words = "";

            bool tens = false;

            if (n < 0)
            {
                words += "negative ";
                n *= -1;
            }

            int power = (suffixesArr.Length+1)*3;

            while (power > 3)
            {
                double pow = Math.Pow(10, power);
                if (n > pow)
                {
                    if (n % Math.Pow(10, power) > 0)
                    {
                        words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
                    }
                    else if (n % pow > 0)
                    {
                        words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
                    }
                    n %= pow;
                }
                power -= 3;
            }
            if (n >= 1000)
            {
                if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " thousand, ";
                else words += NumWords(Math.Floor(n / 1000)) + " thousand";
                n %= 1000;
            }
            if (0 <= n && n <= 999)
            {
                if ((int)n / 100 > 0)
                {
                    words += NumWords(Math.Floor(n / 100)) + " hundred";
                    n %= 100;
                }
                if ((int)n / 10 > 1)
                {
                    if (words != "")
                        words += " ";
                    words += tensArr[(int)n / 10 - 2];
                    tens = true;
                    n %= 10;
                }

                if (n < 20 && n > 0)
                {
                    if (words != "" && tens == false)
                        words += " ";
                    words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
                    n -= Math.Floor(n);
                }
            }

            return words;

        }
        static void Main(string[] args)
        {
            Console.Write("Enter a number to convert to words: ");
            Double n = Double.Parse(Console.ReadLine());

            Console.WriteLine("{0}", NumWordsWrapper(n));
        }
    }
}


Edited by EC_Joe, 29 January 2013 - 09:02 AM.
Fixed Index Out of bounds for numbers ending in 0

Joe Meyer
ExchangeCore Administration
joe@exchangecore.com / (888) 777-6732

#2 Hassan Khan

Hassan Khan

    EC Newbie

  • Members
  • Pip
  • 2 posts

Posted 29 January 2013 - 02:19 AM

When i enter

100

1000

10000

 

This error occurs

 

"Index was outside the bounds of the array."

 

 

Please fix it.


Edited by EC_Joe, 29 January 2013 - 09:05 AM.
Removed email addresses from post


#3 EC_Joe

EC_Joe

    Root Administrator

  • EC-Owner
  • PipPipPipPipPipPipPip
  • 253 posts

Posted 29 January 2013 - 09:04 AM

I have fixed the program so it now allows for numbers ending in 0.  There was an issue in this section of the code:

                if (n < 20)
                {
                    if (words != "" && tens == false)
                        words += " ";
                    words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
                    n -= Math.Floor(n);
                }

It has been changed to the following, notice that we don't need to check a number if it's zero we just carry on our way and this was my oversight:

                if (n < 20 && n > 0)
                {
                    if (words != "" && tens == false)
                        words += " ";
                    words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
                    n -= Math.Floor(n);
                }

Joe Meyer
ExchangeCore Administration
joe@exchangecore.com / (888) 777-6732

#4 Hassan Khan

Hassan Khan

    EC Newbie

  • Members
  • Pip
  • 2 posts

Posted 02 February 2013 - 12:58 PM

It shows

100000 = one hundred thousand not One Lack

Please fix it if you can

 

Thankssssssssssssss in advance



#5 EC_Joe

EC_Joe

    Root Administrator

  • EC-Owner
  • PipPipPipPipPipPipPip
  • 253 posts

Posted 02 February 2013 - 06:24 PM

It shows

100000 = one hundred thousand not One Lack

Please fix it if you can

 

Thankssssssssssssss in advance

 

Currently this program is written for English only.  If you're after something else you can feel free to make the changes yourself; it shouldn't be to difficult as most of the structure is already written, you will just need to modify a few of the strings. Good Luck!


Joe Meyer
ExchangeCore Administration
joe@exchangecore.com / (888) 777-6732





Also tagged with one or more of these keywords: C# Console Application

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users