Can any one help me to write a C# code to convert the numbers into words

Posted by Johnseelan under C# on 9/22/2012 | Points: 10 | Views : 2634 | Status : [Member] | Replies : 6
i can give any numbers
if i give 123 as integer i need out put like
Hundred and Twenty Nine
please help me it is urgent




Responses

Posted by: Hariinakoti on: 9/22/2012 [Member] Starter | Points: 25

Up
0
Down
Hi John,
try like this
int intNo = 123;
string strNo = Convert.toString(intNo);

Thanks & Regards
Hari

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/22/2012 [Member] Starter | Points: 25

Up
0
Down
Please Mark As Answer if u got solution with my answer.

Thanks & Regards
Hari

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Perumal93 on: 9/22/2012 [Member] Starter | Points: 25

Up
0
Down
@Hariinakoti Your answer is right for some other reason. He want the result in words when he give input in numbers. If he give 150 then the result in words are hundred and fifty. Like the result he want.

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Perumal93 on: 9/22/2012 [Member] Starter | Points: 25

Up
0
Down
@ Hariinakoti Your code converts the integer type value to string type.

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vikash on: 9/22/2012 [Member] Starter | Points: 25

Up
0
Down
Plz try following 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 = new System.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();
}
}

Plz mark answer if it was helpful... As it helps other to get better........

Regards,
Vikash Pathak

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 9/24/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Refer this url and download sample code
http://www.codeproject.com/Articles/15934/How-to-convert-a-numeric-value-or-currency-to-Engl

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response