



Hi!!!!
how can I convert words into numerals...In first textbox i enter word and on button click equivalent numeral is displayed in another textbox
for Example we enter "One crore twenty lakh thirty four thousand seven hundred eighty four" ,
then its corresponding numeral should be "12034784".
What will be the C# code for finding corresponding numerals for range 0 to 999999999 .....
I mean kindly give me general code applicable for all values.........
how can I convert words into numerals...In first textbox i enter word and on button click equivalent numeral is displayed in another textbox
for Example we enter "One crore twenty lakh thirty four thousand seven hundred eighty four" ,
then its corresponding numeral should be "12034784".
What will be the C# code for finding corresponding numerals for range 0 to 999999999 .....
I mean kindly give me general code applicable for all values.........

Sanjay Chauhan55 reputationPosted Aug 11, 2009, 7:29 PM EDT
Mark it as Accepted answer if it helps you.
protected void Button1_Click(object sender, EventArgs e)
{
Hashtable hashTable = new Hashtable();
hashTable.Add("one", "1");
hashTable.Add("two", "2");
hashTable.Add("three", "3");
hashTable.Add("four", "4");
hashTable.Add("five", "5");
hashTable.Add("six", "6");
hashTable.Add("seven", "7");
hashTable.Add("eight", "8");
hashTable.Add("nine", "9");
hashTable.Add("ten", "10");
hashTable.Add("eleven", "11");
hashTable.Add("twelve", "12");
hashTable.Add("thirteen", "13");
hashTable.Add("fourteen", "14");
hashTable.Add("fifteen", "15");
hashTable.Add("sixteen", "16");
hashTable.Add("seventeen", "17");
hashTable.Add("eighteen", "18");
hashTable.Add("nineteen", "19");
hashTable.Add("twenty", "20");
hashTable.Add("thirty", "30");
hashTable.Add("fourty", "40");
hashTable.Add("fifty", "50");
hashTable.Add("sixty", "60");
hashTable.Add("seventy", "70");
hashTable.Add("eighty", "80");
hashTable.Add("ninety", "90");
hashTable.Add("hundred", "100");
hashTable.Add("thousand", "1000");
hashTable.Add("lakh", "100000");
hashTable.Add("crore", "10000000");
hashTable.Add("minus", "-");
string wordValue = TextBox1.Text.Trim().ToLower();
string[] strArray = wordValue.Split(' ');
int value = 0;
int tempValue = 0;
bool flag = false;
foreach (string s in strArray)
{
if (object.Equals(s, "hundred") || object.Equals(s, "thousand") || object.Equals(s, "lakh") || object.Equals(s, "crore"))
{
value += tempValue * int.Parse(hashTable[s].ToString());
tempValue = 0;
}
else if (object.Equals(s, "minus"))
{
flag = true;
}
else
{
if (hashTable.ContainsKey(s))
{
tempValue += int.Parse(hashTable[s].ToString());
}
}
}
if (!object.Equals(wordValue, "") || !object.Equals(wordValue, "zero"))
{
if (value + tempValue == 0)
{
Label1.Text = "Enter text in correct format!";
}
else
{
if (flag)
TextBox1.Text = "-" + (value + tempValue).ToString();
else
TextBox1.Text = (value + tempValue).ToString();
}
}
}
fitUOM UOM29 reputationPosted Aug 10, 2009, 6:00 PM EDT
There is not simple method to convert number to its text equal ant.
Unfortunately, the .NET framework does not provide it.
Bt u can implement the method easily. try out with this...
public string IntegerToWords(long inputNum) { int dig1,dig2,dig3,level=0,lasttwo,threeDigits; string retval=""; string x=""; string[] ones={ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; string[] tens={ "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; string[] thou={ "", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion" }; bool isNegative=false; if (inputNum true; inputNum*=-1; } if (inputNum==0) return ("zero"); string s=inputNum.ToString(); while (s.Length>0) { // Get the three rightmost characters x=(s.Length// Separate the three digits threeDigits=int.Parse(x); lasttwo=threeDigits % 100; dig1=threeDigits / 100; dig2=lasttwo / 10; dig3=(threeDigits % 10); // append a "thousand" where appropriate if (level>0 && dig1+dig2+dig3>0) { retval=thou[level] + " " + retval; retval=retval.Trim(); } // check that the last two digits is not a zero if (lasttwo>0) { if (lasttwo// if less than 20, use "ones" only retval=ones[lasttwo] + " " + retval; else // otherwise, use both "tens" and "ones" array retval=tens[dig2] + " " + ones[dig3] + " " + retval; } // if a hundreds part is there, translate it if (dig1>0) retval = ones[dig1] + " hundred " + retval; s=(s.Length - 3)>0 ? s.Substring(0,s.Length - 3) : ""; level++; } while (retval.IndexOf(" ")>0) retval=retval.Replace(" "," "); retval=retval.Trim(); if (isNegative) retval="negative " + retval; return (retval); }u can cll to this methode by
vinni jain109 reputationPosted Aug 10, 2009, 8:07 PM EDT
I want its opposite code i.e.words to number conversion code
Master BillaRank #2,229 · 630 reputationPosted Aug 10, 2009, 9:29 PM EDT
You can use this code
protected void Button1_Click(object sender, EventArgs e) { txtWord.Text=ConvertNumberToWord(Convert.ToInt64(txtNumber.Text)); } private string ConvertNumberToWord(long nNumber) { long CurrentNumber = nNumber; string sReturn = ""; if(CurrentNumber >= 1000000000) { sReturn = sReturn + " " + GetWord(CurrentNumber/1000000000, "Billion"); CurrentNumber = CurrentNumber % 1000000000; } if(CurrentNumber >= 1000000) { sReturn = sReturn + " " + GetWord(CurrentNumber/1000000, "Million"); CurrentNumber = CurrentNumber % 1000000; } if(CurrentNumber >= 1000) { sReturn = sReturn + " " + GetWord(CurrentNumber/1000, "Thousand"); CurrentNumber = CurrentNumber % 1000; } if(CurrentNumber >= 100) { sReturn = sReturn + " " + GetWord(CurrentNumber/100, "Hundred"); CurrentNumber = CurrentNumber % 100; } if(CurrentNumber >= 20) { sReturn = sReturn + " " + GetWord(CurrentNumber, ""); CurrentNumber = CurrentNumber % 10; } else if(CurrentNumber > 0) { sReturn = sReturn + " " + GetWord(CurrentNumber, ""); CurrentNumber = 0; } return sReturn.Replace(" ", " ").Trim(); } private string GetWord(long nNumber, string sPrefix) { long nCurrentNumber = nNumber; string sReturn = ""; while(nCurrentNumber > 0) { if(nCurrentNumber > 100) { sReturn = sReturn + " " + GetWord(nCurrentNumber / 100, "Hundred"); nCurrentNumber = nCurrentNumber % 100; } else if(nCurrentNumber > 20) { sReturn = sReturn + " " + GetTwentyWord(nCurrentNumber / 10); nCurrentNumber = nCurrentNumber % 10; } else { sReturn = sReturn + " " + GetLessThanTwentyWord(nCurrentNumber); nCurrentNumber = 0; } } sReturn = sReturn + " " + sPrefix; return sReturn; } private string GetTwentyWord(long nNumber) { string sReturn = ""; switch(nNumber) { case 2: sReturn = "Twenty"; break; case 3: sReturn = "Thirty"; break; case 4: sReturn = "Forty"; break; case 5: sReturn = "Fifty"; break; case 6: sReturn = "Sixty"; break; case 7: sReturn = "Seventy"; break; case 8: sReturn = "Eighty"; break; case 9: sReturn = "Ninety"; break; } return sReturn; } private string GetLessThanTwentyWord(long nNumber) { string sReturn = ""; switch(nNumber) { case 1: sReturn = "One"; break; case 2: sReturn = "Two"; break; case 3: sReturn = "Three"; break; case 4: sReturn = "Four"; break; case 5: sReturn = "Five"; break; case 6: sReturn = "Six"; break; case 7: sReturn = "Seven"; break; case 8: sReturn = "Eight"; break; case 9: sReturn = "Nine"; break; case 10: sReturn = "Ten"; break; case 11: sReturn = "Eleven"; break; case 12: sReturn = "Twelve"; break; case 13: sReturn = "Thirteen"; break; case 14: sReturn = "Forteen"; break; case 15: sReturn = "Fifteen"; break; case 16: sReturn = "Sixteen"; break; case 17: sReturn = "Seventeen"; break; case 18: sReturn = "Eighteen"; break; case 19: sReturn = "Nineteen"; break; } return sReturn; }vinni jain109 reputationPosted Aug 10, 2009, 11:09 PM EDT
In the first textbox , user enters a string like this : "twenty five lac thirty seven thousand nine hundred and fifty"..
So on button click He should get its equivalent number : " 2537950"............
Tell me the code for such application...........
Danatas GerviRank #4,152 · 361 reputationPosted Aug 11, 2009, 12:52 AM EDT
The question is "Should it be solved?" I think - no.
I mean - if you are geting this string from outer source - e-mail, txt file or something - you cannot garantee, that your algorithm (any kind of it) will give correct result. What you will do in case of detected error? Will you ask again to resend this string to you? How? By e-mail or phone?
What wil happens in case of undetected errors? To act (apply this getted number) according so long user input - this will allow to bad boys to make an injection....
if you are getting this string from same propram source - user input (textbox), and you want to be sure that user input is correct, it will be better to replace this text box by number of comboboxes, or numeric upDown controls and make something like this
And, when user has finished this input simple collect the result from controls, and ack him "Do you really want to pay me twenty five billions dollar?" :)
String for asking you can generate by code, that was showed to you above.
:)
Sanjay Chauhan55 reputationPosted Aug 11, 2009, 3:01 AM EDT
this is the solution of your problem.
protected void Button1_Click(object sender, EventArgs e)
{
Hashtable hashTable = new Hashtable();
hashTable.Add("one", "1");
hashTable.Add("two", "2");
hashTable.Add("three", "3");
hashTable.Add("four", "4");
hashTable.Add("five", "5");
hashTable.Add("six", "6");
hashTable.Add("seven", "7");
hashTable.Add("eight", "8");
hashTable.Add("nine", "9");
hashTable.Add("ten", "10");
hashTable.Add("eleven", "11");
hashTable.Add("twelve", "12");
hashTable.Add("thirteen", "13");
hashTable.Add("fourteen", "14");
hashTable.Add("fifteen", "15");
hashTable.Add("sixteen", "16");
hashTable.Add("seventeen", "17");
hashTable.Add("eighteen", "18");
hashTable.Add("nineteen", "19");
hashTable.Add("twenty", "20");
hashTable.Add("thirty", "30");
hashTable.Add("fourty", "40");
hashTable.Add("fifty", "50");
hashTable.Add("sixty", "60");
hashTable.Add("seventy", "70");
hashTable.Add("eighty", "80");
hashTable.Add("ninety", "90");
hashTable.Add("hundred", "100");
hashTable.Add("thousand", "1000");
hashTable.Add("lakh", "100000");
hashTable.Add("crore", "10000000");
string wordValue = TextBox1.Text.Trim().ToLower();
string[] strArray = wordValue.Split(' ');
int value = 0;
int tempValue = 0;
foreach (string s in strArray)
{
if (object.Equals(s, "hundred") || object.Equals(s, "thousand") || object.Equals(s, "lakh") || object.Equals(s, "crore"))
{
value += tempValue * int.Parse(hashTable[s].ToString());
tempValue = 0;
}
else
{
if (hashTable.ContainsKey(s))
{
tempValue += int.Parse(hashTable[s].ToString());
}
}
}
TextBox1.Text = (value + tempValue).ToString();
}
Happy Coding!!
vinni jain109 reputationPosted Aug 11, 2009, 3:58 AM EDT
Its giving error for hashtable:
Error: The type or namespace name 'HashTable' could not be found (are you missing a using directive or an assembly reference?)
Sanjay Chauhan55 reputationPosted Aug 11, 2009, 4:05 AM EDT
include namesapce for the hashtable by using this.
using System.Collections;
vinni jain109 reputationPosted Aug 11, 2009, 4:17 AM EDT
Still its giving error.......
vinni jain109 reputationPosted Aug 11, 2009, 4:23 AM EDT
I have made small error in spelling which I have corrected........
Now its working fine.
Thanks a lot........
I think people on this forum are very intelligent and helpful..........Because no other forum was able to solve my problem........
Sanjay Chauhan55 reputationPosted Aug 11, 2009, 4:24 AM EDT
As i think u have included this namespace
using System.Collections.Generic;
not
using System.Collections;
Also include this namespace
vinni jain109 reputationPosted Aug 11, 2009, 4:41 AM EDT
iN THIS CODE IF i ENTER "tenthousand" i.e both the words joined together.......
In this case I want to display error message "enter the words with proper spacing"........
Also if someone writes "minus ten thousand" , how to replace minus with its respective sign.
Master BillaRank #2,229 · 630 reputationPosted Aug 11, 2009, 5:13 AM EDT
Just add another collection with all possible signs, then find from, check if user enter char is exists in the your sign collection, then add word according that sign to your text box.
Thank you
vinni jain109 reputationPosted Aug 11, 2009, 10:47 AM EDT
I tried adding hashtable.Add("minus","-");
But its giving error.......
Also if i enter words without spacing................
Its giving zero answer instead I want message box to be dispalyed as"enter values in correct format".......How to handle this exception.........
vinni jain109 reputationPosted Aug 11, 2009, 9:05 PM EDT
vinni jain109 reputationPosted Aug 14, 2009, 5:25 AM EDT
In this code ,I added for million and billion also.But the problem is that its giving wrong answer for:"One million two hundred eighty six thousand four hundred seventy five"...
How can i modify my code for such input...
kindly give me some solution for this.......
vinni jain109 reputationPosted Aug 18, 2009, 8:14 PM EDT
In this code ,I added for million and billion also.But the problem is that its giving wrong answer for:"One million two hundred eighty six thousand four hundred seventy five"...
Here,I am confused how the system will interpret hundred ,which is occuring just after million ,differently...
How can i modify my code for such input...
kindly give me some solution for this.......
vinni jain109 reputationPosted Aug 18, 2009, 8:19 PM EDT
I want to ask one thing....
In this code ,I added for million and billion also.But the problem is that its giving wrong answer for:"One million two hundred eighty six thousand four hundred seventy five"...
Here,I am confused how the system will interpret hundred ,which is occuring just after million ,differently...
How can i modify my code for such input...
kindly give me some solution for this.......
vinni jain109 reputationPosted Aug 19, 2009, 1:23 AM EDT
Kindly reply to my thread..........
himanshu gupta4 reputationPosted Apr 23, 2010, 1:35 AM EDT
himanshu gupta4 reputationPosted Apr 23, 2010, 1:38 AM EDT