473,836 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2625
VJ,

Have you tried the static Parse method on the Int32 structure?
"VJ" <vi********@yah oo.com> wrote in message
news:OJ******** ********@TK2MSF TNGP12.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(te xtbox.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.c om> wrote in
message news:O$******** ******@TK2MSFTN GP15.phx.gbl...
VJ,

Have you tried the static Parse method on the Int32 structure?
"VJ" <vi********@yah oo.com> wrote in message
news:OJ******** ********@TK2MSF TNGP12.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******** ******@TK2MSFTN GP12.phx.gbl...
I think he's going the other direction, Nicholas, ie:

double number = double.Parse(te xtbox.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******** ******@TK2MSFTN GP12.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(s tring 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.Spli t('.');

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

// trillions ------------------------------------------
if(Convert.ToIn t32(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.ToIn t32(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.ToIn t32(astrNumber[0].Substring(1, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(1, 1) + "0")];
if(Convert.ToIn t32(astrNumber[0].Substring(2, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(2, 1))];
}
}
strOutput += " trillion ";
}

// billions ------------------------------------------
if(Convert.ToIn t32(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.ToIn t32(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.ToIn t32(astrNumber[0].Substring(4, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(4, 1) + "0")];
if(Convert.ToIn t32(astrNumber[0].Substring(5, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(5, 1))];
}
}
strOutput += " billion ";
}

// millions ------------------------------------------
if(Convert.ToIn t32(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.ToIn t32(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.ToIn t32(astrNumber[0].Substring(7, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(7, 1) + "0")];
if(Convert.ToIn t32(astrNumber[0].Substring(8, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(8, 1))];
}
}
strOutput += " million ";
}

// thousands ------------------------------------------
if(Convert.ToIn t32(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.ToIn t32(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.ToIn t32(astrNumber[0].Substring(10, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(10, 1) + "0")];
if(Convert.ToIn t32(astrNumber[0].Substring(11, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(11, 1))];
}
}
strOutput += " thousand ";
}

// hundreds, tens and ones ---------------------------------------------
if(Convert.ToIn t32(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.ToIn t32(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.ToIn t32(astrNumber[0].Substring(13, 2)) > 19)
{
strOutput += " and " +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(13, 1) + "0")];
if(Convert.ToIn t32(astrNumber[0].Substring(14, 1)) > 0)
{
strOutput += "-" +
htblNumbers[Convert.ToInt32 (astrNumber[0].Substring(14, 1))];
}
}
}

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

return strOutput.Repla ce(" ", " ");
}
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********@yah oo.com> wrote in message
news:OJ******** ********@TK2MSF TNGP12.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
2170
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 (given a sequence with a len of N that gives the digits to use) as "a single expression". I haven't found a really general way, much less a clear one, but, the best I have so far is...: def number_in_base(x, N, digits, maxlen=99): return...
2
2081
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 can find out this information? thanks
9
52920
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
1530
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 to show me how many board feet of lumber to buy (expression is as follows): =( * * /144) * (Val()/4) If you're wondering what the Val statement is for, it's to grab the
6
3272
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 spaces then add one to get the number of words then number_of_rows = 1; while getline (in, line) and number_of_rows++ does the number of rows I am not sure how to go about the number of words task, is there a c++
3
6357
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, tabs, linefeeds, returns). It will print 3 pieces of information before exit: the first word, the last word, and the number of words read, with one blank in between fields. Input lines of words Output
4
1529
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 return the number of the index, i only get the number of the char. I hope that some1 can help me. Its written in C# console. Like this: A line of words. Then type one of the words like : line . Than i need to get returned 1....
6
2286
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 size of the files if it is better to do so. So the idea is to search through a string and find repeats of any 3 or 4 word group. So if the author has repeated the phrase "then I went" 6 times in the text, then this would be found and...
2
3420
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 results of the analysis are to be stored in a suitable data structure. The main task in this project is to design a suitable ADT (call it WAnalysis) to store the results and enable the following operations to be performed as fast as possible:...
0
9820
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10846
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10551
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4458
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.