473,386 Members | 1,819 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,386 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 6046
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jorfei | last post by:
I have written a component with a property IPAdrress of type System.Net.IPAddress. To ease the configuration of the component at design time, I have written a type converter for the type...
2
by: TheMadHatter | last post by:
does anybody know of a half decent converter????? I tried the "VBConversions VB.Net to C# Converter" with less than satisfactory results, and an unnecisary hole in the bank.
4
by: John | last post by:
Hi Is there a way to convert whole c# projects (or solutions) to vb.net projects or solutions? Most converters I have come across only convert snippets. Thanks Regards
1
by: Nikola | last post by:
Hellooo! Can someone help me please. I tipe this code and it don't work!!! Whyyyyyy. <html> <head> <title>Text object value</title> <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> <!--...
5
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
12
by: Tana | last post by:
Hi, My company wants to migrate all our apps from vb.net to c#. Can someone recommend a good migrate/convert tool? I am hoping that such a tool can do a 90-95% work for me, and I will do the...
10
by: esha | last post by:
I tried several online converters. In many case they do the job, but sometimes give some mess. I think that all converters I know are old, were created for VS 2003 and do not understand new stuff...
2
by: Goofy | last post by:
How to discover how many lines of code are in a dotnet assembly ? -- Goofy
3
by: John Dalberg | last post by:
I have been trying a few commerical vb.net to c# converters. None of them converts inline vb.net asp.net code? Is there any converter that can convert inline code (code in aspx files)? I don't...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.