Click here to Skip to main content
14,216,711 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I am using VS 2008 using web application.

how to convert number into words in c#.

Regards
Mukesh
Posted
Comments
_Amy 5-Aug-14 10:46am
   
Your question is not informative at all. what do you mean by number to words? Click on Improve Question(Green Button) and provide the details.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Did you search at all? You didn't, did you? There are many many many articles and tips here that provide that.

I hadn't realized that school was picking up again already. It seems this is a perennial first year class assignment. And then all the students are so proud of their creations that they post them here.

Similarly, please consider that teachers know about this site and will know if you simply copied (plagiarized) any of these implementations.
Furthermore, none of these implementations is perfect (including Solution 2), but you can read each and use the concepts you like to form a solution that is truly your own.


Convert Currency To Words in Lakh, Crores, etc.[^]

Convert Amount in Words and Separate Amount with comma in Bangladeshi Currency Format using C#.NET[^]

XSLT Number to String Transformation II[^]

Convert Numeric Currency into Words for International Currency - Part - II (Optimized)[^]


We have so many we even have a spoof: Converting numbers to the word equivalent. [^]
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

   
Comments
PIEBALDconsult 5-Aug-14 11:18am
   
Ooooh, sorry, that links to another site. Naughty naughty.
Mehdi Gholam 5-Aug-14 11:27am
   
Amazingly it was the first in Google (I was looking for the ones in CP honest! :))
PIEBALDconsult 5-Aug-14 11:29am
   
OK, I'll let you off easy this time. I keep the links I posted in a bookmark because I have to post them so frequently. :(
Afzaal Ahmad Zeeshan 20-Aug-14 11:02am
   
hehe you can also try to copy/paste the content (source code) here too :P with a little elaboration. ;)
Rate this:
Please Sign up or sign in to vote.

Solution 2

static int input;
static int temp;
static int index = 0;
static int[] buffer = new int[10];

static void ones(int value)//to print numbers in ones
{
      switch (value)
        {
            case 1: Console.Write(" one"); break;
            case 2: Console.Write(" Two"); break;
            case 3: Console.Write(" three"); break;
            case 4: Console.Write(" Four"); break;
            case 5: Console.Write(" Five"); break;
            case 6: Console.Write(" Six"); break;
            case 7: Console.Write(" Seven"); break;
            case 8: Console.Write(" Eight"); break;
            case 9: Console.Write(" Nine"); break;
            default: break;
        }
           }

static void teens(int value)//to print numbers which are in teens
{
                  switch (value)
        {
            case 0: Console.Write("ten"); break;
            case 1: Console.Write(" eleven"); break;
            case 2: Console.Write(" Twelve"); break;
            case 3: Console.Write(" thirteen"); break;
            case 4: Console.Write(" Fourteen"); break;
            case 5: Console.Write(" Fifteen"); break;
            case 6: Console.Write(" Sixteen"); break;
            case 7: Console.Write(" Seventeen"); break;
            case 8: Console.Write(" Eighteen"); break;
            case 9: Console.Write(" Nineteen"); break;
        }
    }


static void tens(int value)//to print tens values
{
        switch (value)
        {
            case 2: Console.Write(" Twenty"); break;
            case 3: Console.Write(" thirty"); break;
            case 4: Console.Write(" Forty"); break;
            case 5: Console.Write(" Fifty"); break;
            case 6: Console.Write(" Sixty"); break;
            case 7: Console.Write(" Seventy"); break;
            case 8: Console.Write(" Eighty"); break;
            case 9: Console.Write(" Ninty"); break;
            default: break;
        }
    }


static void Main(string[] args)
{
        Console.WriteLine("Enter a number to convert it into words");
        input = Convert.ToInt32(Console.ReadLine());//input from the user
        do
        {
            buffer[index] = input % 10;
            input /= 10;
            index++;
        } while (input != 0);
        wordgeneration();
        Console.ReadLine();
}

private static void wordgeneration()
{
                   for (temp = index - 1; temp >= 0; temp--)
        {
            if (temp == 5)//check for the values in lakhs
            {
                ones(buffer[temp]);
                Console.Write("Lakhs");
            }
            else if (temp == 3)//check for the numbers in thousands
            {
                if (buffer[temp + 1] == 1)
                {
                    teens(buffer[temp]);
                    Console.Write("Thousand");
                }
                else
                {
                    tens(buffer[temp + 1]);
                    ones(buffer[temp]);
                    if (!((buffer[temp + 1] == 0) && (buffer[temp] == 0)))
                    {
                        Console.Write("Thousand");
                    }
                }
            }
            else if (temp == 2)//check for the numbers in hundreds
            {
                if (buffer[temp] != 0)
                {
                    ones(buffer[temp]);
                    Console.Write("Hundred");
                }
            }
            else if (temp == 0)//chcek for the numbers in tens and ones
            {
                if (buffer[temp + 1] == 1)
                    teens(buffer[temp]);
                else
                {
                    tens(buffer[temp + 1]);
                    ones(buffer[temp]);

                }
            }

        }
    }

Convert this to web application.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 7

using System;
using System.Linq;

namespace CSN2W
{
    class Program
    {
        static void Main(string[] args)
        {
            string result;
            int i, num;
            int[] arrNum =
              {
                -1, 0, 5, 10, 15, 19, 20, 21, 25, 33, 49, 50, 72,
                99, 100, 101, 117, 199, 200, 214, 517, 589, 999,5700,
                1000, 1010, 1018, 1200, 9890, 10119, 13535, 57019,
                99999, 100000, 100001,1900001,19000010,1111111111,59899999
              };

            for (i = 0; i < arrNum.Count(); i++)
            {
                num = arrNum[i];
                result = NumberToWord(num);
                Console.WriteLine(num + "\t" + result);
            }
            Console.ReadKey();
        }


        static string NumberToWord(int num)
        {
            if (num == 0)
                return "Zero";

            if (num < 0)
                return "Not supported";

            var words = "";
            string[] strones = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
            string[] strtens = { "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };


            int crore = 0, lakhs = 0, thousands = 0, hundreds = 0, tens = 0, single = 0;


            crore = num / 10000000; num = num - crore * 10000000;
            lakhs = num / 100000;   num = num - lakhs * 100000;
            thousands = num / 1000; num = num - thousands * 1000;
            hundreds = num / 100;   num = num - hundreds * 100;
            if (num > 19)
            {
                tens = num / 10;    num = num - tens * 10;
            }
            single = num;


            if (crore > 0)
            {
                if (crore > 19)
                    words += NumberToWord(crore) + "Crore ";
                else
                    words += strones[crore - 1] + " Crore ";
            }

            if (lakhs > 0)
            {
                if (lakhs > 19)
                    words += NumberToWord(lakhs) + "Lakh ";
                else
                    words += strones[lakhs - 1] + " Lakh ";
            }

            if (thousands > 0)
            {
                if (thousands > 19)
                    words += NumberToWord(thousands) + "Thousand ";
                else
                    words += strones[thousands - 1] + " Thousand ";
            }

            if (hundreds > 0)
                words += strones[hundreds - 1] + " Hundred ";

            if (tens > 0)
                words += strtens[tens - 2] + " ";

            if (single > 0)
                words += strones[single - 1] + " ";

            return words;
        }
    }
}
   
v3
Comments
CHill60 21-Jul-15 6:48am
   
Please don't resurrect old questions that are already resolved. Your solution brings nothing new to the other solutions.
Rate this:
Please Sign up or sign in to vote.

Solution 8

Try this

http://www.codeproject.com/Tips/568673/Numbers-to-Words-in-Csharp-SQL-Server-and-Crystal
   
v2
Comments
Matt T Heffron 24-Nov-15 12:54pm
   
As was noted above 5 months ago:
Please don't resurrect old questions that are already resolved.
CHill60 25-Nov-15 5:47am
   
Further to the comment above, posting links to your own article as solutions to old, resolved posts, is usually considered to be abuse of the site

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100