473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Integer to Word

Tyler Wiebe
This is just something I created since I couldn't find anything to do it.

It converts an integer (Example: 1495954485) to the word representation of that number (Example: one billion four hundred ninety five million nine hundred fifty four thousand four hundred eighty five).

Expand|Select|Wrap|Line Numbers
  1.     public class Integer
  2.     {
  3.         #region Functions
  4.  
  5.         /// <summary>
  6.         /// Returns the total amount of digits in the value.
  7.         /// </summary>
  8.         /// <param name="Value">The desired value to get the length from.</param>
  9.         /// <returns>Returns the total amount of digits in the value.</returns>
  10.         public static int ToLenth(int Value)
  11.         {
  12.             return Value.ToString().Length;
  13.         }
  14.         /// <summary>
  15.         /// Returns the integer at the desired index position.
  16.         /// </summary>
  17.         /// <param name="Value">The desired value to get the index value from.</param>
  18.         /// <param name="Index">The index position to get the desired integer from.</param>
  19.         /// <returns>Returns the integer at the desired index position.</returns>
  20.         public static int ToIndex(int Value, int Index)
  21.         {
  22.             return int.Parse(Value.ToString()[Index - 1].ToString());
  23.         }
  24.         /// <summary>
  25.         /// Returns the integer within the first and second index positions.
  26.         /// </summary>
  27.         /// <param name="Value">The desired value to get the return value from within the first and second index values.</param>
  28.         /// <param name="Index1">The index position of the beginning of the return value.</param>
  29.         /// <param name="Index2">The index position of the end of the return value.</param>
  30.         /// <returns>Returns the integer within the first and second index positions.</returns>
  31.         public static int ToSubIndex(int Value, int Index1, int Index2)
  32.         {
  33.             Index1 = Index1 - 1;
  34.             if (Index2 == 0) return int.Parse(Value.ToString().Substring(Index1, ToLenth(Value) - Index1));
  35.             else
  36.             {
  37.                 Index2 = System.Math.Abs(Index1 - Index2);
  38.                 return int.Parse(Value.ToString().Substring(Index1, Index2));
  39.             }
  40.         }
  41.  
  42.         private static bool CheckValue(int Value)
  43.         {
  44.             if (Value > int.MinValue && Value < int.MaxValue) return true;
  45.             else
  46.             {
  47.                 if (Value >= 0) throw new System.OutOfMemoryException("The value \"" + Value + "\" is to large to convert. Maximum Value = " + (int.MaxValue - 1));
  48.                 else throw new System.OutOfMemoryException("The value \"" + Value + "\" is to small to convert. Minimum Value = " + (int.MinValue + 1));
  49.             }
  50.         }
  51.         private static string GetSingleDigit(int Value)
  52.         {
  53.             switch (Value)
  54.             {
  55.                 case 0: return "zero";
  56.                 case 1: return "one";
  57.                 case 2: return "two";
  58.                 case 3: return "three";
  59.                 case 4: return "four";
  60.                 case 5: return "five";
  61.                 case 6: return "six";
  62.                 case 7: return "seven";
  63.                 case 8: return "eight";
  64.                 case 9: return "nine";
  65.                 default: return null;
  66.             }
  67.         }
  68.         private static string GetDoubleDigit(int Value)
  69.         {
  70.             if (Value < 20)
  71.             {
  72.                 switch (Value)
  73.                 {
  74.                     case 10: return "ten";
  75.                     case 11: return "eleven";
  76.                     case 12: return "twelve";
  77.                     case 13: return "thirteen";
  78.                     case 14: return "fourteen";
  79.                     case 15: return "fifteen";
  80.                     case 16: return "sixteen";
  81.                     case 17: return "seventeen";
  82.                     case 18: return "eighteen";
  83.                     case 19: return "nineteen";
  84.                     default: return null;
  85.                 }
  86.             }
  87.             else
  88.             {
  89.                 string Word = null;
  90.                 switch (ToIndex(Value, 1))
  91.                 {
  92.                     case 2: Word = "twenty"; break;
  93.                     case 3: Word = "thirty"; break;
  94.                     case 4: Word = "fourty"; break;
  95.                     case 5: Word = "fifty"; break;
  96.                     case 6: Word = "sixty"; break;
  97.                     case 7: Word = "seventy"; break;
  98.                     case 8: Word = "eighty"; break;
  99.                     case 9: Word = "ninety"; break;
  100.                     default: return null;
  101.                 }
  102.  
  103.                 if (!string.IsNullOrEmpty(Word) && ToIndex(Value, 2) != 0) Word += " " + GetSingleDigit(ToIndex(Value, 2));
  104.                 return Word;
  105.             }
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Returns the desired integer as a word.
  110.         /// </summary>
  111.         /// <param name="Value">The desired integer that will be returned as a word.</param>
  112.         /// <returns>Returns the desired integer as a word.</returns>
  113.         public static string ToWord(int Value)
  114.         {
  115.             CheckValue(Value);
  116.             string Word = null;
  117.  
  118.             if (Value < 0) Word = "minus ";
  119.             Value = System.Math.Abs(Value);
  120.  
  121.             int Value1 = 0;
  122.             int Value2 = 0;
  123.  
  124.             switch (ToLenth(Value))
  125.             {
  126.                 case 1: return Word += GetSingleDigit(Value);
  127.                 case 2: return Word += GetDoubleDigit(Value);
  128.                 case 3:
  129.                     Value1 = ToIndex(Value, 1);
  130.                     Value2 = ToSubIndex(Value, 2, 0);
  131.  
  132.                     Word += GetSingleDigit(Value1) + " hundred";
  133.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  134.                     return Word;
  135.                 case 4:
  136.                     Value1 = ToIndex(Value, 1);
  137.                     Value2 = ToSubIndex(Value, 2, 0);
  138.  
  139.                     Word += GetSingleDigit(Value1) + " thousand";
  140.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  141.                     return Word;
  142.                 case 5:
  143.                     Value1 = ToSubIndex(Value, 1, 2);
  144.                     Value2 = ToSubIndex(Value, 3, 0);
  145.  
  146.                     Word += GetDoubleDigit(Value1) + " thousand";
  147.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  148.                     return Word;
  149.                 case 6:
  150.                     Value1 = ToSubIndex(Value, 1, 3);
  151.                     Value2 = ToSubIndex(Value, 4, 0);
  152.  
  153.                     Word += ToWord(Value1) + " thousand";
  154.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  155.                     return Word;
  156.                 case 7:
  157.                     Value1 = ToIndex(Value, 1);
  158.                     Value2 = ToSubIndex(Value, 2, 0);
  159.  
  160.                     Word += ToWord(Value1) + " million";
  161.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  162.                     return Word;
  163.                 case 8:
  164.                     Value1 = ToSubIndex(Value, 1, 2);
  165.                     Value2 = ToSubIndex(Value, 3, 0);
  166.  
  167.                     Word += ToWord(Value1) + " million";
  168.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  169.                     return Word;
  170.                 case 9:
  171.                     Value1 = ToSubIndex(Value, 1, 3);
  172.                     Value2 = ToSubIndex(Value, 4, 0);
  173.  
  174.                     Word += ToWord(Value1) + " million";
  175.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  176.                     return Word;
  177.                 case 10:
  178.                     Value1 = ToIndex(Value, 1);
  179.                     Value2 = ToSubIndex(Value, 2, 0);
  180.  
  181.                     Word += GetSingleDigit(Value1) + " billion";
  182.                     if (Value2 > 0) Word += " " + ToWord(Value2);
  183.                     return Word;
  184.                 default: return Word;
  185.             }
  186.         }
  187.  
  188.         #endregion
  189.     }
  190.  
Sorry if the summary on these functions don't make sense, I'm not really good at giving descriptions.

Anyways, I had originally made more code that would create a list of words, which I then converted to what I remember as System.Speech.Choices with a foreach loop for a speech recognition program I was working on to control my computers master volume.

Making a list of words from zero to one hundred was easy enough and didn't a lot of memory, but the reason my functions for making a list aren't here is because I've decided that they aren't complete yet, and probably never will, mostly for the following reasons:
#1: Any programer can just as easily make a list and add words with a for loop.
#2: If anyone attempted to make a large list, it'd take up to much memory, trust me, I tried.

I even made a custom list collection that would only store the integer value, and then have a function to convert that value to the word, which was still pretty much a for loop. Even with only storing integers instead of strings, I could only store 134217728 values. Which is a lot, but since that number can change from system to system, I decided to leave the list functions out.

Anyways, let me know what you think. Any improvement ideas are welcome.

And to tell you the truth, I still have no idea what this could be used for, I just thought it'd be handy to have if I ever needed it.
Feb 7 '12 #1
0 4236

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Salgoud Dell | last post by:
C++ has a variable type 'unsigned short int.' I am trying to write a VB6 program that prints these out of a file made originally by C++. However, the unsigned short integer (1-65535) is not...
8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
1
by: Ronny Sigo | last post by:
Hello all, I want to create a new word document from VBA (Access). All goes well, except that I also want to establish a background programmatically. So I made a macro is MS WORD, to see how it's...
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
1
by: Adam Faulkner via DotNetMonster.com | last post by:
I had a problem before extracting pages from an existing word document and then inserting the content into a new word document. The following code below works with Microsoft Word 2000 Function...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: raypjr | last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
5
by: sejal17 | last post by:
Hello Friends, I want Application that displays an image preview of a Word document (Like thumbnail View of word document) Following is my code but i got error on bold part of code ...
9
by: jacob navia | last post by:
Hi I am incorporating 128 Bit integer code into lcc-win and it would be nice to have some code to test this feature. Has anyone here code that uses 128 bit integers? Thanks in advance ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.