



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 ChauhanPosted Aug 11, 2009, 11:29 PM
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();
}
}
}
himanshu guptaPosted Apr 23, 2010, 5:38 AM
himanshu guptaPosted Apr 23, 2010, 5:35 AM
vinni jainPosted Aug 19, 2009, 5:23 AM
Kindly reply to my thread..........
vinni jainPosted Aug 19, 2009, 12:19 AM
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 jainPosted Aug 19, 2009, 12:14 AM
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 jainPosted Aug 14, 2009, 9:25 AM
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 jainPosted Aug 12, 2009, 1:05 AM
vinni jainPosted Aug 11, 2009, 2:47 PM
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.........
Master BillaPosted Aug 11, 2009, 9:13 AM
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 jainPosted Aug 11, 2009, 8:41 AM
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.
Sanjay ChauhanPosted Aug 11, 2009, 8:24 AM
As i think u have included this namespace
using System.Collections.Generic;
not
using System.Collections;
Also include this namespace
vinni jainPosted Aug 11, 2009, 8:23 AM
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........
vinni jainPosted Aug 11, 2009, 8:17 AM
Still its giving error.......
Sanjay ChauhanPosted Aug 11, 2009, 8:05 AM
include namesapce for the hashtable by using this.
using System.Collections;
vinni jainPosted Aug 11, 2009, 7:58 AM
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 ChauhanPosted Aug 11, 2009, 7:01 AM
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!!
Danatas GerviPosted Aug 11, 2009, 4:52 AM
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.
:)
vinni jainPosted Aug 11, 2009, 3:09 AM
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...........
Master BillaPosted Aug 11, 2009, 1:29 AM
You can use this code
vinni jainPosted Aug 11, 2009, 12:07 AM
I want its opposite code i.e.words to number conversion code
fitUOM UOMPosted Aug 10, 2009, 10:00 PM
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...
u can cll to this methode by