Number to Word Conversion using C#

This blog shows the number to word Conversion.

User entered 1020304

It prints ten lakhs twenty thousand three hundred four only in words

Console.Write("Enter the no : ");

int s = int.Parse(Console.ReadLine());

num n = new num();

string ss= n.Getword(s);

Console.WriteLine(ss);

Console.ReadKey();

Image-1.jpg

The function GETWORD defined as follows
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace number_to_words

{

    public class num

    {

        public string Getword(int value)

        {

            string rstr = "";

            int crores = value / 10000000;

            value = value % 10000000;

            int lakh = value / 100000;

            value = value % 100000;

            int thos = value / 1000;

            value = value % 1000;

            int hund = value / 100;

            value = value % 100;

            int ten = value;

 

            if (crores != 0)

            {

                rstr = rstr + Getwordhund(crores) + "crores";

            }

 

            if (lakh != 0)

            {

                rstr = rstr + Getwordhund(lakh) + " lakhs ";

            }

 

            if (thos != 0)

            {

                rstr = rstr + Getwordhund(thos) + " thousands ";

            }

 

            if (hund != 0)

            {

                rstr = rstr + Getwordhund(hund) + " hundreds ";

            }

 

            rstr = rstr + Getwordhund(ten) + "only ";

            return rstr;

        }

 

        public static string Getwordhund(int value)

        {

            List<string> num = new List<string>();

            num.Add(" ");

            num.Add(" one ");

            num.Add(" two ");

            num.Add(" three ");

            num.Add(" four ");

            num.Add(" five ");

            num.Add(" six ");

            num.Add(" seven ");

            num.Add(" eight ");

            num.Add(" nine ");

            num.Add(" ten ");

            num.Add(" eleven ");

            num.Add(" twelve ");

            num.Add(" thirteen ");

            num.Add(" fourteen ");

            num.Add(" fifteen ");

            num.Add(" sixteen ");

            num.Add(" seventeen ");

            num.Add(" eighteen ");

            num.Add(" nineteen ");

            List<string> ten = new List<string>();

            ten.Add(" ");

            ten.Add(" ten ");

            ten.Add(" twenty ");

            ten.Add(" thirty ");

            ten.Add(" forty ");

            ten.Add(" fifty ");

            ten.Add(" sixty ");

            ten.Add(" seventy ");

            ten.Add(" eighty ");

            ten.Add(" ninty ");

            if (value <= 19)

            {

                return num[value];

            }

            else if (value % 10 != 0)

            {

                return ten[value / 10] + num[value % 10];

            }

            else

            {

                return ten[value / 10] + num[value % 10];

            }

        }

    }

}

Ebook Download
View all
Learn
View all