473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Number to Words..

VJ
Is there any known component that would convert a value entered as 40,000 to
Forty Thousand..

Thanks
VJ
Nov 17 '05 #1
5 2604
VJ,

Have you tried the static Parse method on the Int32 structure?
"VJ" <vi********@yahoo.com> wrote in message
news:OJ****************@TK2MSFTNGP12.phx.gbl...
Is there any known component that would convert a value entered as 40,000
to Forty Thousand..

Thanks
VJ

Nov 17 '05 #2
I think he's going the other direction, Nicholas, ie:

double number = double.Parse(textbox.Text);
// number contains 40000
string words = ConvertToWords(number);
// words contains "Forty thousand"

.... and no, I don't know of anything that'll do that. Sorry.

Scott

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O$**************@TK2MSFTNGP15.phx.gbl...
VJ,

Have you tried the static Parse method on the Int32 structure?
"VJ" <vi********@yahoo.com> wrote in message
news:OJ****************@TK2MSFTNGP12.phx.gbl...
Is there any known component that would convert a value entered as 40,000
to Forty Thousand..

Thanks
VJ


Nov 17 '05 #3
"Scott Coonce" <sd******@gmail.HEY_YOU.com> wrote in message
news:Ov**************@TK2MSFTNGP12.phx.gbl...
I think he's going the other direction, Nicholas, ie:

double number = double.Parse(textbox.Text);
// number contains 40000
string words = ConvertToWords(number);
// words contains "Forty thousand"

... and no, I don't know of anything that'll do that. Sorry.


Indeed. This is a really simple, though incredibly long-winded procedure
involving an array thus:

0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
....
....
....
20 twenty
21 twenty-one
....
....
....
99 ninety-nine

1) You start off by stripping all the formatting e.g. 123,456,789.012 ->
123456789.012

2) Then you count the number of digits to the left of the decimal point, in
this case, 7 and consider them in groups of three padded with leading zeroes
e.g. 123 456 789

3) Next you take the first group (which you know to be "millions" because of
the number of digits to the left of the decimal point is 7) and convert it
to an integer e.g. 123.

If this value is less than 100, look it up in the array and append the
result to your output string.
If the value is 100 or more, do an integer division by 100 on it which, in
this case, returns 1. Look that up in the array i.e. "one" and append that
suffixed with " hundred" to your output string.
Do a Mod 100 on the value which, in this case, returns 23. Look that up in
the array i.e. "twenty-three" and append that prefixed with " and " and
suffixed with " million" to your output string.

Your output string now contains "one hundred and twenty-three million"

4) Do the same with with the thousands

5) Do the same with the hundreds, tens and ones.

6) Check if the original value contains a decimal point (that's a period to
our American friends!)
If so, add the word " point " to your output string, which now contains

"one hundred and twenty-three million four hundred and fifty-six thousand
seven hundred and eighty-nine point"

Finally, add the digits after the decimal point one by one.

Final result is:

"one hundred and twenty-three million four hundred and fifty-six thousand
seven hundred and eighty-nine point zero one two"
I'm sure that such an algorithm already exists on the net somewhere. No
doubt banks use something similar when printing cheques (that's checks to
our American friends!)
Nov 17 '05 #4
"Scott Coonce" <sd******@gmail.HEY_YOU.com> wrote in message
news:Ov**************@TK2MSFTNGP12.phx.gbl...
... and no, I don't know of anything that'll do that. Sorry.


Below is an extremely crude piece of code which will do the job. Before you
all jump down my throat at how inefficient and inelegant it is, you're
absolutely right! It's not intended to be an exercise in concise coding,
rather an example that the OP can use to modify as required...

private string NumberToWords(string pstrNumber)
{
Hashtable htblNumbers = new Hashtable();
htblNumbers.Add(0, "zero");
htblNumbers.Add(1, "one");
htblNumbers.Add(2, "two");
htblNumbers.Add(3, "three");
htblNumbers.Add(4, "four");
htblNumbers.Add(5, "five");
htblNumbers.Add(6, "six");
htblNumbers.Add(7, "seven");
htblNumbers.Add(8, "eight");
htblNumbers.Add(9, "nine");
htblNumbers.Add(10, "ten");
htblNumbers.Add(11, "eleven");
htblNumbers.Add(12, "twelve");
htblNumbers.Add(13, "thirteen");
htblNumbers.Add(14, "fourteen");
htblNumbers.Add(15, "fifteen");
htblNumbers.Add(16, "sixteen");
htblNumbers.Add(17, "seventeen");
htblNumbers.Add(18, "eighteen");
htblNumbers.Add(19, "nineteen");
htblNumbers.Add(20, "twenty");
htblNumbers.Add(30, "thirty");
htblNumbers.Add(40, "forty");
htblNumbers.Add(50, "fifty");
htblNumbers.Add(60, "sixty");
htblNumbers.Add(70, "seventy");
htblNumbers.Add(80, "eighty");
htblNumbers.Add(90, "ninety");

string strOutput = "";
string[] astrNumber = pstrNumber.Split('.');

if(astrNumber[0].Length > 15)
{
return "Number is too long to convert";
}
else
{
astrNumber[0] = astrNumber[0].PadLeft(15, '0');
}

// trillions ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(0, 3)) > 0)
{
if(astrNumber[0].Substring(0, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(0,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(1, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(1, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(1, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(1, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(1, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(2, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(2, 1))];
}
}
strOutput += " trillion ";
}

// billions ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(3, 3)) > 0)
{
if(astrNumber[0].Substring(3, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(3,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(4, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(4, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(4, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(4, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(4, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(5, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(5, 1))];
}
}
strOutput += " billion ";
}

// millions ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(6, 3)) > 0)
{
if(astrNumber[0].Substring(6, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(6,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(7, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(7, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(7, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(7, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(7, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(8, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(8, 1))];
}
}
strOutput += " million ";
}

// thousands ------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(9, 3)) > 0)
{
if(astrNumber[0].Substring(9, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(9,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(10, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(10, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(10, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(10, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(10, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(11, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(11, 1))];
}
}
strOutput += " thousand ";
}

// hundreds, tens and ones ---------------------------------------------
if(Convert.ToInt32(astrNumber[0].Substring(12, 3)) > 0)
{
if(astrNumber[0].Substring(12, 1) != "1")
{
strOutput += htblNumbers[Convert.ToInt32(astrNumber[0].Substring(12,
1))] + " hundred";
}
if(Convert.ToInt32(astrNumber[0].Substring(13, 2)) > 0 &&
Convert.ToInt32(astrNumber[0].Substring(13, 2)) < 20)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(13, 2))];
}
if(Convert.ToInt32(astrNumber[0].Substring(13, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(13, 1) + "0")];
if(Convert.ToInt32(astrNumber[0].Substring(14, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32(astrNumber[0].Substring(14, 1))];
}
}
}

// decimal places ------------------------------------------------------
if(astrNumber.Length == 2)
{
strOutput += " point";
foreach (char strDigit in astrNumber[1].ToCharArray())
{
strOutput += " " + htblNumbers[Convert.ToInt32(strDigit.ToString())];
}
}

return strOutput.Replace(" ", " ");
}
Nov 17 '05 #5
VJ
Thanks Every one for the Input.... I will try and build what you have
given.. If I find some alogrithm I will share with group.!!
VJ

"VJ" <vi********@yahoo.com> wrote in message
news:OJ****************@TK2MSFTNGP12.phx.gbl...
Is there any known component that would convert a value entered as 40,000
to Forty Thousand..

Thanks
VJ

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Alex Martelli | last post by:
I hesitate a bit to post this, but... on the Italian Python NG, somebody was asking whether there was any way to convert an integer number x into a string which represents it in an arbitrary base N...
2
by: janet | last post by:
how can i count how many words have i written in a text area??? Like taking an example ... i am writing in this textarea of microsoft usergroup. and say in total i have written 50 words .. how...
9
by: cw bebop | last post by:
Hi all Using Visual Studio C# Have a string string st = "Hi, these pretzels are making me thirsty; drink this tea. Run like heck." ******
4
by: JaredEmery | last post by:
Hello all, It's my first database, and I have a query that shows me the quantity, material, length, width and thicknesses of parts, and I'm using these figures to do some arithmetic on a report...
6
by: Gary Wessle | last post by:
hi I have a data file with equal number of columns for each row. I need to get the number of rows and columns to allocate a matrix in gsl. getline (in, line) and parse the line for the number of...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: Words are delimited by white spaces (blanks,...
4
by: Robba | last post by:
I got a little problem, i have to return the number of the array of the word that is filled in. First there is filled in a sentence, and have to be split. I think i splitted it but now i cant...
6
by: jeddiki | last post by:
I am writing a little script that will improve authors writing skills by finding repeated phrases in the text. The text of a chapter will average about 10,000 words, however, I could reduce the...
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.