472,119 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Number to English Converter

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.Length-1) + "ieth";
}
}
return returnString;
}

-Andrew Arace
Nov 16 '05 #1
1 5948
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.Length-1) + "ieth";
}
}
return returnString;
}

-Andrew Arace

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by jorfei | last post: by
2 posts views Thread by TheMadHatter | last post: by
4 posts views Thread by John | last post: by
1 post views Thread by Nikola | last post: by
5 posts views Thread by Pavils Jurjans | last post: by
12 posts views Thread by Tana | last post: by
10 posts views Thread by esha | last post: by
2 posts views Thread by Goofy | last post: by
3 posts views Thread by John Dalberg | last post: by
reply views Thread by leo001 | last post: by

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.