Number to word conversion in Csharp

In this article we can convert any number to its respective words format. Here in this example we can convert it up to 4 digits only.

 

Program

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Number_to_word_in_csharp

{

    public partial class Form1 : Form

    {

        int n = 0;

        string x = null;

      

 

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Button1_Click(object sender, EventArgs e)

        {

            string[] a = {

      "One",

      "Two",

      "Three",

      "four",

      "Five",

      "Six",

      "Seven",

      "Eight",

      "Nine",

      "Ten",

      "Eleven",

      "Twelve",

      "Thirteen",

      "fourteen",

      "Fifteen",

      "Sixteen",

      "Seventeen",

      "Eighteen",

      "Ninteen"

};

 

            string[] b = {

      "Twenty",

      "Thirty",

      "Fourty",

      "Fifty",

      "sixty",

      "Seventy",

      "eighty",

      "ninty"

};

 

            x = "";

            n = int.Parse(textBox1.Text);

            if ((n <= 9999))

            {

                if ((n > 999 & n <= 9999))

                {

                    x += a[(n / 1000) - 1] + "Thousand";

                    n = n % 1000;

                }

                x += " ";

                if ((n > 99 & n <= 999))

                {

                    x += a[(n / 100) - 1] + "Hundred";

                    n = n % 100;

                }

                x += " ";

                if ((n > 19 & n <= 99))

                {

                    x += b[(n / 10) - 2];

                    n = n % 10;

                }

                x += " ";

                if ((n > 0 & n <= 19))

                {

                    x += a[n - 1];

                }

 

                textBox2.Text = x;

               

            }

            else

            {

                textBox2.Text = ("Number is out of range");

              

            }

 

        }

    }

}

 

Output

Thanks for reading.

Ebook Download
View all
FileInfo in C#
Read by 9.7k people
Download Now!
Learn
View all