473,614 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integer to Word

Tyler Wiebe
66 New Member
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.C hoices 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 4256

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

Similar topics

2
11152
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 supported. Does anyone have a routine that will allow VB6 to read a C++ unsigned short int? Thanks Del
8
3021
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 whether it was docked or not. I added the following code to my "OnConnection" function but it fails with an error, "Run-time exception thrown : System.IO.IOException - Bad file name or number." With applicationObject.CommandBars("SampleToolbar")
1
8352
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 done and then pasted the macrolines in my code, but it does not work ... (those are the "ActiveDocument" lines below ....) Does anyone know how to do this ? Thanks ... Ronny
3
30774
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 Document and save it to a new Word Document. I would need to generate a loop for each page found within a word document to create a new word document and insert the existing page into the new word document and then save as a new word document. ...
1
7635
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 ParseWordDoc(ByVal Filename As String) As String Dim sNewFileName As String Dim WordApp As Word.Application = New Word.Application Dim BaseDoc As Word.Document Dim DestDoc As Word.Document
1
13084
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 the other....how i can fix this the program look like this import java.util.ArrayList; import java.util.Random;
0
1708
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 appreciated.Here is the code to give more detail: Dim GameOver As Boolean Dim NumWords As Integer, ThreeWordList(1000) As String, ThreeWordMeaning(1000) As String Dim R As Integer, WordsLeft(1000) As Integer Dim SecretWord As String,...
4
12427
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
5
12044
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 Imports System Imports System.Text
9
12603
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 P.S. This feature is now native in the 64 bit version, i.e.
0
8130
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8275
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8429
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6088
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4050
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2566
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
0
1423
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.