Forum guidelines
AuthorQuestion
Covert the numeric value to words in C#
Posted on: 24 Jul 2012
How to convert 123 to words(one thousands twenty three) in c#

AuthorReply
Re: Covert the numeric value to words in C#
Posted on: 24 Jul 2012  
Hi,

try this............


static void Main(string[] args)
        {
            Console.WriteLine("---Number to Words---");
            Console.WriteLine("==========================");
            Console.WriteLine("Enter number");
            string Number = Console.ReadLine();
            int number2 = Int32.Parse(Number);
            string word = ConvertNumberToWords(number2);
            Console.WriteLine("Number in words are: {0}", word);
            Console.ReadLine();
        }

private static string ConvertNumberToWords(int number)
        {
            if (number == 0)
                return "zero";

            if (number < 0)
                return "minus" + ConvertNumberToWords(Math.Abs(number));

            string words = string.Empty;

            if ((number / 1000000) > 0)
            {
                words += ConvertNumberToWords(number / 1000000) + " milllion ";
            }

            if ((number / 1000) > 0)
            {
                words += ConvertNumberToWords(number / 1000) + " thousand ";
                number %= 1000;
            }

            if ((number / 100) > 0)
            {
                words += ConvertNumberToWords(number / 100) + " hundred ";
                number %= 100;
            }

            if (number > 0)
            {
                if (words != "")
                    words += "and ";

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

                if (number < 20)
                    words += unitsMap[number];
                else
                {
                    words += tensMap[number / 10];
                    if ((number % 10) > 0)
                        words += "-" + unitsMap[number % 10];
                }
            }

            return words;

        }

Thanks and Regards Naresh Avari
Re: Covert the numeric value to words in C#
Posted on: 24 Jul 2012  
number to word in csharp.rar Hi Sharad,


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");
              
            }

        }
    }
}



Thanks
If this post helps you mark it as answer

If this post helps you mark it as answer
Thanks

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.