473,561 Members | 3,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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[][] _englishDigitMa trix = 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">a ny integer, positive or negative, within the
bounds of int.</param>
/// <param name="place">tr ue to set to ordered positions, such as
"second" instead of "two"</param>
/// <returns>englis h 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 += _englishDigitMa trix[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 += _englishDigitMa trix[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 += _englishDigitMa trix[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 += _englishDigitMa trix[tempNumber][1];
}
else {
returnString += _englishDigitMa trix[tempNumber][0];
}
}
else if (tempNumber == 0 && returnString.Le ngth == 0) {
//need to catch the solitary number 0
//nothing will have been caught before this,
//so returnString will be empty.
if(order) {
returnString += _englishDigitMa trix[tempNumber][1];
}
else {
returnString += _englishDigitMa trix[tempNumber][0];
}
}
}
returnString = returnString.Tr im();
//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("bill ion") ||
returnString.En dsWith("million ") ||
returnString.En dsWith("thousan d") ||
returnString.En dsWith("hundred ")) {
returnString += "th";
}
else if(!single && !teen && tens) {
//must be multiple of 10, greater than or equal to 20
//less than onehundred
returnString = returnString.Su bstring(0,
returnString.Le ngth-1) + "ieth";
}
}
return returnString;
}

-Andrew Arace
Nov 16 '05 #1
1 6058
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**********@g mail.com> wrote in message
news:26******** *************** ***@posting.goo gle.com...
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[][] _englishDigitMa trix = 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">a ny integer, positive or negative, within the
bounds of int.</param>
/// <param name="place">tr ue to set to ordered positions, such as
"second" instead of "two"</param>
/// <returns>englis h 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 += _englishDigitMa trix[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 += _englishDigitMa trix[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 += _englishDigitMa trix[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 += _englishDigitMa trix[tempNumber][1];
}
else {
returnString += _englishDigitMa trix[tempNumber][0];
}
}
else if (tempNumber == 0 && returnString.Le ngth == 0) {
//need to catch the solitary number 0
//nothing will have been caught before this,
//so returnString will be empty.
if(order) {
returnString += _englishDigitMa trix[tempNumber][1];
}
else {
returnString += _englishDigitMa trix[tempNumber][0];
}
}
}
returnString = returnString.Tr im();
//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("bill ion") ||
returnString.En dsWith("million ") ||
returnString.En dsWith("thousan d") ||
returnString.En dsWith("hundred ")) {
returnString += "th";
}
else if(!single && !teen && tens) {
//must be multiple of 10, greater than or equal to 20
//less than onehundred
returnString = returnString.Su bstring(0,
returnString.Le ngth-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
2912
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 System.Net.IPAddress. The type System.Net.IPAddress, as you know, is provided by Microsoft and I have no choice but to apply the type converter on the...
2
3717
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
2160
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
1882
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"> <!-- function upperMe() { document.converter.ulaz.value =
5
2294
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 internally convert it so that it could be saved in database as real number (float). I plan to provide property(set) interface for reading the value.
12
2646
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 rest :) I am using VS 2005. thanks
10
2470
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 from VS 2005. For instance this block containing Using was not converted by anyone: Public Shared Function GetSiteSettings() As SiteSettings Using...
2
906
by: Goofy | last post by:
How to discover how many lines of code are in a dotnet assembly ? -- Goofy
3
3733
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 want to manually move inline code to code behind unless there's a utlity which automates this process. John Dalberg
0
7648
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...
0
7573
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8091
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...
1
7620
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7931
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...
0
6213
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...
1
5473
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...
0
3602
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2069
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.