Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how convert integer value to words:
eg: 1020=Thousand and twenty
Posted
Comments
[no name] 4-Sep-12 3:00am    
I can give you all the code, but what have you tried?

try this

Call
AmountInWords obj=new AmountInWords();
obj.Convert(10450.67)


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
Comments
amirmohamad 4-Sep-12 3:29am    
vote 5+
Ganesh Nikam 4-Sep-12 4:22am    
thanks
rajin kp 8-Oct-12 2:47am    
THANKS
Take a look at this CP article
Convert numbers to words in English and Asian format[^]

Convert VB.net code to C# here[^]
 
Share this answer
 
Comments
__TR__ 4-Sep-12 3:27am    
Why downvoted ?
amirmohamad 4-Sep-12 3:38am    
5+
__TR__ 4-Sep-12 3:48am    
Thanks.
Mohamed Mitwalli 5-Sep-12 5:43am    
5+
__TR__ 5-Sep-12 6:11am    
Thanks Mohamed.
I have written the program to convert currency to string.
little bit modifications to this program will do your work.

C#
class Program
    {
        static void Main(string[] args)
        {
            string ammount=string .Empty ;
        Rep:
            Console.WriteLine("Enter the Ammount");
            ammount = Console.ReadLine();
            Regex r=new Regex (@"^[0-9]+(\.[0-9]+)?$");
            if (!r.IsMatch(ammount)|ammount .Length >9)
            {
                Console.Clear();
                Console.WriteLine("Enter Valid Number within Thousand Core\n");
                goto Rep;
            }
            string inWord = ConvertIntoCurrency(ammount.Trim());
            if (inWord == "Error")
            {
                Console.WriteLine("Enter Valid Number within Thousand Core\n");
                goto Rep;
            }
            Console.WriteLine("Ammount in words : "+inWord);
            Console.ReadLine();
        }


        //Function To Convert the Ammount
        static  string ConvertIntoCurrency(string ammount)
        {
            string inWords=string .Empty;
            int dec=0,cur,ix;
            cur = (int)double.Parse(ammount);
            string[] words = { ".Hundred.", ".Thousand.", ".Thousand.", ".Lakh.", ".Lakh.", ".Core.", ".Core." };
            int[] devidors = { 100, 1000, 1000, 100000, 100000, 10000000, 10000000 };

            if (ammount.IndexOf('.') > 0)
            {
                dec = int.Parse(ammount.Substring(ammount.IndexOf('.') + 1));

                if (dec >= 100)
                {
                    cur += dec / 100;
                    dec = dec % 100;
                }
            }

            if (cur == 0 & dec > 0)
                return dec.ToString() + ".Paise";
            if (cur == 0 & dec == 0)
                return "Nill";
            if (cur.ToString().Length > 9)
                return "Error";


           while (cur.ToString().Length > 2)
           {
               ix = cur.ToString().Length-3;
               inWords += (cur / devidors[ix]).ToString() + words[ix]+"And.";
               cur = cur % devidors[ix];
           }

           if (cur > 0)
           {
                inWords += cur.ToString();
           }

            if (inWords.LastIndexOf(".And.")==inWords .Length -5)
                inWords = inWords.Remove(inWords.Length - 5);

            if (cur == 1)
                inWords += ".Rupee";
            else
                inWords += ".Rupees";

            if (dec > 0)
                inWords += ".And." + dec.ToString() + ".Paise";
            return inWords;
        }

    }
 
Share this answer
 
Comments
amirmohamad 4-Sep-12 3:29am    
5+
rajin kp 8-Oct-12 2:48am    
THANKS
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;

      }
 
Share this answer
 
Comments
rajin kp 8-Oct-12 2:48am    
THANKS

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