Here is code to present the number (digits) in word format.
Suppose we have amount in number : Rs. 10101 then some time requirement is to present this amount in words,
in such situation we can do this by using following code......... like ten thousand one hundred & one......
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
int n = 0;
string x = null;
protected 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
{
TextBox1.Text = ("Number is out of range");
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
int n = 0;
string x = null;
protected 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
{
TextBox1.Text = ("Number is out of range");
}
}
}
A question which is often asked on
programming forums is - how do I convert a number to words?
Although many solutions have been posted over
the years, we appear to be lacking a basic article on the subject here on C#
Corner.
In this article, I'd therefore like to
present a simple program which I've written to deal with this question for
integers in the range of an Int32 (about plus or minus 2 billion) which I
believe is the most useful case.
The program supports both the US and UK
numbering systems. For example the number 620 would be expressed as follows:
Six Hundred Twenty
(in the US system)
Six Hundred and Twenty
(in the UK system)
Source Code
using System;
class Program
{
static void Main()
{
string input;
int number;
bool isValid;
bool isUK = false;
Console.WriteLine("\nEnter '0' to quit the program at any time\n");
while (true)
{
Console.Write("\nUse UK numbering y/n : ");
input = Console.ReadLine();
if (!(input.ToLower() == "y" || input.ToLower() == "n"))
Console.WriteLine("\n Must be 'y' or 'n',
please try again\n");
else
{
if (input.ToLower()
== "y") isUK = true;
Console.WriteLine("\n");
break;
}
}
do
{
Console.Write("Enter integer : ");
input = Console.ReadLine();
isValid = int.TryParse(input, out number);
if (!isValid)
Console.WriteLine("\n Not an integer, please
try again\n");
else
Console.WriteLine("\n {0}\n", NumberToText(number, isUK));
}
while (!(isValid && number == 0));
Console.WriteLine("\nProgram ended");
}
public static string NumberToText(int number, bool isUK)
{
if (number == 0) return "Zero";
string and = isUK ? "and " : ""; //
deals with UK or US numbering
if (number == -2147483648) return "Minus
Two Billion One Hundred " +
and +
"Forty Seven Million
Four Hundred " +
and + "Eighty
Three Thousand " +
"Six Hundred " + and + "Forty Eight";
int[] num = new int[4];
int first = 0;
int u, h, t;
System.Text.StringBuilder sb = newSystem.Text.StringBuilder();
if (number < 0)
{
sb.Append("Minus
");
number = -number;
}
string[] words0 = {"", "One
", "Two ", "Three ", "Four
", "Five ","Six ", "Seven
", "Eight ", "Nine "};
string[] words1 = {"Ten ", "Eleven
", "Twelve ", "Thirteen ","Fourteen
", "Fifteen ", "Sixteen ", "Seventeen
", "Eighteen ","Nineteen "};
string[] words2 = {"Twenty ", "Thirty
", "Forty ", "Fifty ","Sixty
", "Seventy ", "Eighty ", "Ninety
"};
string[] words3 = { "Thousand ", "Million ", "Billion
" };
num[0] = number % 1000; // units
num[1] = number / 1000;
num[2] = number / 1000000;
num[1] = num[1] - 1000 * num[2]; // thousands
num[3] = number / 1000000000; // billions
num[2] = num[2] - 1000 * num[3]; // millions
for (int i
= 3; i > 0; i--)
{
if (num[i] != 0)
{
first = i;
break;
}
}
for (int i
= first; i >= 0; i--)
{
if (num[i] == 0) continue;
u = num[i] %
10; // ones
t
= num[i] / 10;
h = num[i] /
100; // hundreds
t = t - 10 *
h; // tens
if (h > 0) sb.Append(words0[h] + "Hundred ");
if (u > 0 || t > 0)
{
if (h > 0 || i <
first) sb.Append(and);
if (t == 0)
sb.Append(words0[u]);
else if (t == 1)
sb.Append(words1[u]);
else
sb.Append(words2[t - 2] +
words0[u]);
}
if (i != 0) sb.Append(words3[i - 1]);
}
return sb.ToString().TrimEnd();
}
}
Comments
Post a Comment