Click here to Skip to main content
15,902,714 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am developing a windows application where i need to convert to numbers into text format like if someone enters 6000 then it is converted into six thousand.
please help me.
Posted
Comments
Sandeep Mewara 8-Sep-12 4:04am    
What help you need?

See this short post for C#. I briefly tested it and it works very well:
http://forums.exchangecore.com/topic/684-convert-number-to-words-c-console-application/
You just need to copy the two functions to your code to use it:
C#
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)
            {
               if (words != "" && tens == false)
                  words += " ";
               words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
               n -= Math.Floor(n);
            }
         }

         return words;

      }

or see the other solutions here:
converting Integer To Words[^]
 
Share this answer
 
Hope this will help you.


C#
private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = NoToWords(3500);
        }


        public static string NoToWords(int no)
        {
            if (no == 0)
                return "zero";

            if (no < 0)
                return "minus " + NoToWords(Math.Abs(no));

            string stringValue = "";

            if ((no / 1000000) > 0)
            {
                stringValue += NoToWords(no / 1000000) + " million ";
                no %= 1000000;
            }

            if ((no / 1000) > 0)
            {
                stringValue += NoToWords(no / 1000) + " thousand ";
                no %= 1000;
            }

            if ((no / 100) > 0)
            {
                stringValue += NoToWords(no / 100) + " hundred ";
                no %= 100;
            }

            if (no > 0)
            {
                if (stringValue != "")
                    stringValue += "and ";

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

                if (no < 20)
                    stringValue += units[no];
                else
                {
                    stringValue += tens[no / 10];
                    if ((no % 10) > 0)
                        stringValue += "-" + units[no % 10];
                }
            }

            return stringValue;
        }




Thanks!!!
 
Share this answer
 
hiii,

Define Object as

AmountInWords obj=new AmountInWords();
obj.ConvertAmount(100)

And Output is= One Hundred

where

AmountInWords.cs

C#
class AmountInWords
   {
       private static String[] units = { "Zero", "One", "Two", "Three",
           "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
           "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
           "Seventeen", "Eighteen", "Nineteen" };
       private static String[] tens = { "", "", "Twenty", "Thirty", "Forty",
           "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };


       public String ConvertAmount(double amount)
       {
           try
           {
               int amount_int = (int)amount;
               int amount_dec = (int)Math.Round((amount - (double)(amount_int)) * 100);
               return convert(amount_int) + "  point "
                       + convert(amount_dec);
           }
           catch (Exception e)
           {
               // TODO: handle exception
           }
           return "";
       }

       public String convert(int i)
       {
           //
           if (i < 20)
               return units[i];
           if (i < 100)
               return tens[i / 10] + ((i % 10 > 0) ? " " + convert(i % 10) : "");
           if (i < 1000)
               return units[i / 100] + " Hundred"
                       + ((i % 100 > 0) ? " and " + convert(i % 100) : "");
           if (i < 100000)
               return convert(i / 1000) + " Thousand "
                       + ((i % 1000 > 0) ? " " + convert(i % 1000) : "");
           if (i < 10000000)
               return convert(i / 100000) + " Lakh "
                       + ((i % 100000 > 0) ? " " + convert(i % 100000) : "");
           return convert(i / 10000000) + " Crore "
                   + ((i % 10000000 > 0) ? " " + convert(i % 10000000) : "");
       }

   }
 
Share this answer
 
v2
string input;
int number;
bool isValid;
bool isUK = false;
Console.WriteLine("\nEnter '0' to quit the program at any time\n");
while (true)
{
Console.Write("\nUse UK numbering y/n : ");
input = Console.ReadLine();
if (!(input.ToLower() == "y" || input.ToLower() == "n"))
Console.WriteLine("\n Must be 'y' or 'n', please try again\n");
else
{
if (input.ToLower() == "y") isUK = true;
Console.WriteLine("\n");
break;
}
}
do
{
Console.Write("Enter integer : ");
input = Console.ReadLine();
isValid = int.TryParse(input, out number);
if (!isValid)
Console.WriteLine("\n Not an integer, please try again\n");
else
Console.WriteLine("\n {0}\n", NumberToText(number, isUK));
}
while (!(isValid && number == 0));
Console.WriteLine("\nProgram ended");
}
public static string NumberToText(int number, bool isUK)
{
if (number == 0) return "Zero";
string and = isUK ? "and " : ""; // deals with UK or US numbering
if (number == -2147483648) return "Minus Two Billion One Hundred " + and +
"Forty Seven Million Four Hundred " + and + "Eighty Three Thousand " +
"Six Hundred " + and + "Forty Eight";
int[] num = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (number < 0)
{
sb.Append("Minus ");
number = -number;
}
string[] words0 = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
string[] words3 = { "Thousand ", "Million ", "Billion " };
num[0] = number % 1000; // units
num[1] = number / 1000;
num[2] = number / 1000000;
num[1] = num[1] - 1000 * num[2]; // thousands
num[3] = number / 1000000000; // billions
num[2] = num[2] - 1000 * num[3]; // millions
for (int i = 3; i > 0; i--)
{
if (num[i] != 0)
{
first = i;
break;
}
}
for (int i = first; i >= 0; i--)
{
if (num[i] == 0) continue;
u = num[i] % 10; // ones
t = num[i] / 10;
h = num[i] / 100; // hundreds
t = t - 10 * h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i < first) sb.Append(and);
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] + words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900