Ok this one is an abbreviation of my last post, much shorter, but can only take 3 digits. You don't even need a loop. And you absolutely don't need a hash, you'll need it if you construct a dictionary, but your key
here is an ordered int, so you won't need it.
You will only need the loop if you exceed more than 3 digits (our translation base is only 3 digits long), if you exceed more than this, you would need a tracker for the thousandths, millions, etc (see my first post):
[3 digits] million [3 digits] thousand [3 digits]
and the 3 digits can be broken down to:
x (hundred) -> y z (if less than twenty, belongs to the ones set) or y (tens) -> z (ones)
Code:
public static string IntegerToWords(long inputNum)
{
int dig1, dig2, dig3, lasttwo, threeDigits;
string retval = "";
string x = "";
string[] ones = {"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen",
"nineteen"};
string[] tens = {"zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
if (inputNum == 0)
return ("zero");
string s = inputNum.ToString();
// Sanity check ... s length should not be greater than 3 digits
if (s.Length > 3) return "";
// Separate the three digits
threeDigits = int.Parse(s);
lasttwo = threeDigits%100; // the last 2 digits, is the remainder of 100
dig1 = threeDigits/100; // The hundreths
dig2 = lasttwo/10; // The tens
dig3 = (threeDigits%10); // The ones position
// check that the last two digits is not a zero
if (lasttwo > 0)
{
if (lasttwo < 20) // if less than 20, use "ones" only
retval = ones[lasttwo] + " " + retval;
else // otherwise, use both "tens" and "ones" array
retval = tens[dig2] + " " + ones[dig3] + " " + retval;
}
// if a hundreds part is there, translate it
if (dig1 > 0)
retval = ones[dig1] + " hundred " + retval;
return (retval);
}