P: n/a

I searched for something like this existing already, failing to find
it, I wrote it myself. If this already existed somewhere in the
framework I appologize for my ignorance.
This method will take any number within the bounds of an int
(int.MinValue  int.MaxValue) inclusive and converts it to an english
number.
For example, it will take "1" and give you "one", "2" yields "two",
"475918" give you a whopping "negative four hundred seventy five
thousand nine hundred eighteen"
It can also do what I called "positional numbering" for example given
32, with positional boolean set to true, it will return "thirty
second"
so "1" gives "first"
"2" gives "second", etc...
The method is static, as well as it's support array, so just dump them
into a class anywhere.
/// <summary>
/// A matrix of the oddities in the English counting system
/// such as five > fifth, and all those strange teens
/// </summary>
protected static string[][] _englishDigitMatrix = new string[][]
{
new string[] {"zero", "zeroth", ""},
new string[] {"one", "first", ""},
new string[] {"two", "second", "twenty"},
new string[] {"three", "third", "thirty"},
new string[] {"four", "fourth", "fourty"},
new string[] {"five", "fifth", "fifty"},
new string[] {"six", "sixth", "sixty"},
new string[] {"seven", "seventh", "seventy"},
new string[] {"eight", "eighth", "eighty"},
new string[] {"nine", "nineth", "ninety"},
new string[] {"ten", "", ""},
new string[] {"eleven", "", ""},
new string[] {"twelve", "", ""},
new string[] {"thirteen", "", ""},
new string[] {"fourteen", "", ""},
new string[] {"fifteen", "", ""},
new string[] {"sixteen", "", ""},
new string[] {"seventeen", "", ""},
new string[] {"eighteen", "", ""},
new string[] {"nineteen", "", ""}
};
/// <summary>
/// Converts an integer, within the Max and Min valuse for an int
(inclusive)
/// into English words. The conversion can also use numerical order
positions
/// such as "first" "second", "thirty third" by setting the bool
/// Written by Andrew Arace 10/2004
/// </summary>
/// <param name="number">any integer, positive or negative, within the
bounds of int.</param>
/// <param name="place">true to set to ordered positions, such as
"second" instead of "two"</param>
/// <returns>english string</returns>
protected static string NumberToEnglish(int number, bool order) {
string returnString = string.Empty;
int tempNumber = number;
int countPlace = 0;
int sign = 1;
bool teen = false;
bool single = false;
bool tens = false;
if(number < 0) {
sign *= 1;
returnString += "negative ";
}
//count the billions (int max is over two billion)
countPlace = (tempNumber / 1000000000) * sign;
if(countPlace > 0) {
returnString += NumberToEnglish(countPlace, false) + " billion
";
}
tempNumber = (1000000000 * countPlace) * sign;
//count the millions
countPlace = (tempNumber / 1000000) * sign;
if(countPlace > 0) {
returnString += NumberToEnglish(countPlace, false) + " million
";
}
tempNumber = (1000000 * countPlace) * sign;
//count the thousands
countPlace = (tempNumber / 1000) * sign;
if(countPlace > 0) {
returnString += NumberToEnglish(countPlace, false) + "
thousand ";
}
tempNumber = (1000 * countPlace) * sign;
//any recursion falls in here  in english, the main number
//groupings are in the hundreds  hundreds, hundreds of thousands
//hundreds of millions, etc.
countPlace = (tempNumber / 100) * sign;
if(countPlace > 0) {
returnString += _englishDigitMatrix[countPlace][0] + " hundred
";
}
tempNumber = (100 * countPlace) * sign;
//count the 10's places
countPlace = (tempNumber / 10) * sign;
if(countPlace > 0) {
tens = true;
if(countPlace == 1) {
teen = true;
}
else {
returnString += _englishDigitMatrix[countPlace][2] + " ";
}
}
tempNumber = (10 * countPlace) * sign;
//when working with single digits, and also
//teens, the rules change a bit.
tempNumber *= sign; //for the singles, read positives
if(tempNumber >= 0) {
//catch if we have any single digits
if(tempNumber == 0) {
single = false;
}
else {
single = true;
}
//catch the teens, and the number ten as well
if(teen) {
returnString += _englishDigitMatrix[10 + tempNumber][0];
//catch the position order
if(order) {
returnString += "th";
}
}
else if (tempNumber > 0) {
//catch the position order for single digits
if(order) {
returnString += _englishDigitMatrix[tempNumber][1];
}
else {
returnString += _englishDigitMatrix[tempNumber][0];
}
}
else if (tempNumber == 0 && returnString.Length == 0) {
//need to catch the solitary number 0
//nothing will have been caught before this,
//so returnString will be empty.
if(order) {
returnString += _englishDigitMatrix[tempNumber][1];
}
else {
returnString += _englishDigitMatrix[tempNumber][0];
}
}
}
returnString = returnString.Trim();
//check if it ended on a signifier greater than or
//equal to the hundreds  it won't have any order
//qualifiers, we need to add them
if(order) {
if(returnString.EndsWith("billion") 
returnString.EndsWith("million") 
returnString.EndsWith("thousand") 
returnString.EndsWith("hundred")) {
returnString += "th";
}
else if(!single && !teen && tens) {
//must be multiple of 10, greater than or equal to 20
//less than onehundred
returnString = returnString.Substring(0,
returnString.Length1) + "ieth";
}
}
return returnString;
}
Andrew Arace  
Share this Question
P: n/a

I don't think anything like this exists in the framework  you might wanna
add it as a user sample under www.gotdotnet.com
 Sahil Malik
You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik
"Andrew Arace" <An**********@gmail.com> wrote in message
news:26**************************@posting.google.c om... I searched for something like this existing already, failing to find it, I wrote it myself. If this already existed somewhere in the framework I appologize for my ignorance.
This method will take any number within the bounds of an int (int.MinValue  int.MaxValue) inclusive and converts it to an english number.
For example, it will take "1" and give you "one", "2" yields "two", "475918" give you a whopping "negative four hundred seventy five thousand nine hundred eighteen"
It can also do what I called "positional numbering" for example given 32, with positional boolean set to true, it will return "thirty second" so "1" gives "first" "2" gives "second", etc...
The method is static, as well as it's support array, so just dump them into a class anywhere.
/// <summary> /// A matrix of the oddities in the English counting system /// such as five > fifth, and all those strange teens /// </summary> protected static string[][] _englishDigitMatrix = new string[][] { new string[] {"zero", "zeroth", ""}, new string[] {"one", "first", ""}, new string[] {"two", "second", "twenty"}, new string[] {"three", "third", "thirty"}, new string[] {"four", "fourth", "fourty"}, new string[] {"five", "fifth", "fifty"}, new string[] {"six", "sixth", "sixty"}, new string[] {"seven", "seventh", "seventy"}, new string[] {"eight", "eighth", "eighty"}, new string[] {"nine", "nineth", "ninety"}, new string[] {"ten", "", ""}, new string[] {"eleven", "", ""}, new string[] {"twelve", "", ""}, new string[] {"thirteen", "", ""}, new string[] {"fourteen", "", ""}, new string[] {"fifteen", "", ""}, new string[] {"sixteen", "", ""}, new string[] {"seventeen", "", ""}, new string[] {"eighteen", "", ""}, new string[] {"nineteen", "", ""} };
/// <summary> /// Converts an integer, within the Max and Min valuse for an int (inclusive) /// into English words. The conversion can also use numerical order positions /// such as "first" "second", "thirty third" by setting the bool /// Written by Andrew Arace 10/2004 /// </summary> /// <param name="number">any integer, positive or negative, within the bounds of int.</param> /// <param name="place">true to set to ordered positions, such as "second" instead of "two"</param> /// <returns>english string</returns> protected static string NumberToEnglish(int number, bool order) { string returnString = string.Empty; int tempNumber = number; int countPlace = 0; int sign = 1; bool teen = false; bool single = false; bool tens = false; if(number < 0) { sign *= 1; returnString += "negative "; } //count the billions (int max is over two billion) countPlace = (tempNumber / 1000000000) * sign; if(countPlace > 0) { returnString += NumberToEnglish(countPlace, false) + " billion "; } tempNumber = (1000000000 * countPlace) * sign; //count the millions countPlace = (tempNumber / 1000000) * sign; if(countPlace > 0) { returnString += NumberToEnglish(countPlace, false) + " million "; } tempNumber = (1000000 * countPlace) * sign; //count the thousands countPlace = (tempNumber / 1000) * sign; if(countPlace > 0) { returnString += NumberToEnglish(countPlace, false) + " thousand "; } tempNumber = (1000 * countPlace) * sign; //any recursion falls in here  in english, the main number //groupings are in the hundreds  hundreds, hundreds of thousands //hundreds of millions, etc. countPlace = (tempNumber / 100) * sign; if(countPlace > 0) { returnString += _englishDigitMatrix[countPlace][0] + " hundred "; } tempNumber = (100 * countPlace) * sign;
//count the 10's places countPlace = (tempNumber / 10) * sign; if(countPlace > 0) { tens = true; if(countPlace == 1) { teen = true; } else { returnString += _englishDigitMatrix[countPlace][2] + " "; } } tempNumber = (10 * countPlace) * sign;
//when working with single digits, and also //teens, the rules change a bit. tempNumber *= sign; //for the singles, read positives if(tempNumber >= 0) { //catch if we have any single digits if(tempNumber == 0) { single = false; } else { single = true; } //catch the teens, and the number ten as well if(teen) { returnString += _englishDigitMatrix[10 + tempNumber][0]; //catch the position order if(order) { returnString += "th"; } } else if (tempNumber > 0) { //catch the position order for single digits if(order) { returnString += _englishDigitMatrix[tempNumber][1]; } else { returnString += _englishDigitMatrix[tempNumber][0]; } } else if (tempNumber == 0 && returnString.Length == 0) { //need to catch the solitary number 0 //nothing will have been caught before this, //so returnString will be empty. if(order) { returnString += _englishDigitMatrix[tempNumber][1]; } else { returnString += _englishDigitMatrix[tempNumber][0]; } } } returnString = returnString.Trim(); //check if it ended on a signifier greater than or //equal to the hundreds  it won't have any order //qualifiers, we need to add them if(order) { if(returnString.EndsWith("billion")  returnString.EndsWith("million")  returnString.EndsWith("thousand")  returnString.EndsWith("hundred")) { returnString += "th"; } else if(!single && !teen && tens) { //must be multiple of 10, greater than or equal to 20 //less than onehundred returnString = returnString.Substring(0, returnString.Length1) + "ieth"; } } return returnString; }
Andrew Arace   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 5079
 replies: 1
 date asked: Nov 16 '05
