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).
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.
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).
Code:
public class Integer
{
#region Functions
/// <summary>
/// Returns the total amount of digits in the value.
/// </summary>
/// <param name="Value">The desired value to get the length from.</param>
/// <returns>Returns the total amount of digits in the value.</returns>
public static int ToLenth(int Value)
{
return Value.ToString().Length;
}
/// <summary>
/// Returns the integer at the desired index position.
/// </summary>
/// <param name="Value">The desired value to get the index value from.</param>
/// <param name="Index">The index position to get the desired integer from.</param>
/// <returns>Returns the integer at the desired index position.</returns>
public static int ToIndex(int Value, int Index)
{
return int.Parse(Value.ToString()[Index - 1].ToString());
}
/// <summary>
/// Returns the integer within the first and second index positions.
/// </summary>
/// <param name="Value">The desired value to get the return value from within the first and second index values.</param>
/// <param name="Index1">The index position of the beginning of the return value.</param>
/// <param name="Index2">The index position of the end of the return value.</param>
/// <returns>Returns the integer within the first and second index positions.</returns>
public static int ToSubIndex(int Value, int Index1, int Index2)
{
Index1 = Index1 - 1;
if (Index2 == 0) return int.Parse(Value.ToString().Substring(Index1, ToLenth(Value) - Index1));
else
{
Index2 = System.Math.Abs(Index1 - Index2);
return int.Parse(Value.ToString().Substring(Index1, Index2));
}
}
private static bool CheckValue(int Value)
{
if (Value > int.MinValue && Value < int.MaxValue) return true;
else
{
if (Value >= 0) throw new System.OutOfMemoryException("The value \"" + Value + "\" is to large to convert. Maximum Value = " + (int.MaxValue - 1));
else throw new System.OutOfMemoryException("The value \"" + Value + "\" is to small to convert. Minimum Value = " + (int.MinValue + 1));
}
}
private static string GetSingleDigit(int Value)
{
switch (Value)
{
case 0: return "zero";
case 1: return "one";
case 2: return "two";
case 3: return "three";
case 4: return "four";
case 5: return "five";
case 6: return "six";
case 7: return "seven";
case 8: return "eight";
case 9: return "nine";
default: return null;
}
}
private static string GetDoubleDigit(int Value)
{
if (Value < 20)
{
switch (Value)
{
case 10: return "ten";
case 11: return "eleven";
case 12: return "twelve";
case 13: return "thirteen";
case 14: return "fourteen";
case 15: return "fifteen";
case 16: return "sixteen";
case 17: return "seventeen";
case 18: return "eighteen";
case 19: return "nineteen";
default: return null;
}
}
else
{
string Word = null;
switch (ToIndex(Value, 1))
{
case 2: Word = "twenty"; break;
case 3: Word = "thirty"; break;
case 4: Word = "fourty"; break;
case 5: Word = "fifty"; break;
case 6: Word = "sixty"; break;
case 7: Word = "seventy"; break;
case 8: Word = "eighty"; break;
case 9: Word = "ninety"; break;
default: return null;
}
if (!string.IsNullOrEmpty(Word) && ToIndex(Value, 2) != 0) Word += " " + GetSingleDigit(ToIndex(Value, 2));
return Word;
}
}
/// <summary>
/// Returns the desired integer as a word.
/// </summary>
/// <param name="Value">The desired integer that will be returned as a word.</param>
/// <returns>Returns the desired integer as a word.</returns>
public static string ToWord(int Value)
{
CheckValue(Value);
string Word = null;
if (Value < 0) Word = "minus ";
Value = System.Math.Abs(Value);
int Value1 = 0;
int Value2 = 0;
switch (ToLenth(Value))
{
case 1: return Word += GetSingleDigit(Value);
case 2: return Word += GetDoubleDigit(Value);
case 3:
Value1 = ToIndex(Value, 1);
Value2 = ToSubIndex(Value, 2, 0);
Word += GetSingleDigit(Value1) + " hundred";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
case 4:
Value1 = ToIndex(Value, 1);
Value2 = ToSubIndex(Value, 2, 0);
Word += GetSingleDigit(Value1) + " thousand";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
case 5:
Value1 = ToSubIndex(Value, 1, 2);
Value2 = ToSubIndex(Value, 3, 0);
Word += GetDoubleDigit(Value1) + " thousand";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
case 6:
Value1 = ToSubIndex(Value, 1, 3);
Value2 = ToSubIndex(Value, 4, 0);
Word += ToWord(Value1) + " thousand";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
case 7:
Value1 = ToIndex(Value, 1);
Value2 = ToSubIndex(Value, 2, 0);
Word += ToWord(Value1) + " million";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
case 8:
Value1 = ToSubIndex(Value, 1, 2);
Value2 = ToSubIndex(Value, 3, 0);
Word += ToWord(Value1) + " million";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
case 9:
Value1 = ToSubIndex(Value, 1, 3);
Value2 = ToSubIndex(Value, 4, 0);
Word += ToWord(Value1) + " million";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
case 10:
Value1 = ToIndex(Value, 1);
Value2 = ToSubIndex(Value, 2, 0);
Word += GetSingleDigit(Value1) + " billion";
if (Value2 > 0) Word += " " + ToWord(Value2);
return Word;
default: return Word;
}
}
#endregion
}
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.