intToWords

Java program. These methods convert any number from 0 to 999999 (inclusive) into words with no hyphens. It took about an hour plus small fixes for special cases.

public static String intToWords(int num) {
if (num == 0)
return "zero";
String ans = "";//holds the answer
String bottomDigs = "";//bottom three digits
String topDigs = "";//remaining digits
String numstr = num + "";//change the number into a String
if (numstr.length() > 3) {
bottomDigs = numstr.substring(numstr.length() - 3);
//grab bottom 3 digits
topDigs = numstr.substring(0, numstr.length() - 3);// grab other
//digits
} else {
bottomDigs = numstr;
}
if (topDigs != "") {//if there were thousands
ans += toWords(topDigs);//convert them
ans += "thousand ";
}
ans += toWords(bottomDigs);//convert the rest
return ans;
}

private static String toWords(String digs) {
String retVal = "";
int num = Integer.parseInt(digs);
retVal += oneDig(num / 100);//hundreds digit conversion
if (num / 100 != 0) {//if there were hundreds
retVal += "hundred ";
}
switch (num / 10 % 10) {//checks the tens digit
case 1: retVal += teens(num);
break;
case 2: retVal += "twenty ";
break;
case 3: retVal += "thirty ";
break;
case 4: retVal += "forty ";
break;
case 5: retVal += "fifty ";
break;
case 6: retVal += "sixty ";
break;
case 7: retVal += "seventy ";
break;
case 8: retVal += "eighty ";
break;
case 9: retVal += "ninety ";
break;
default:
//if the tens digit was 0
}
retVal += ((num / 10 % 10) == 1) ? "" : oneDig(num % 10);
return retVal;
}

private static String teens(int num) {
switch (num % 10) {
case 1: return "eleven ";
case 2: return "twelve ";
case 3: return "thirteen ";
case 4: return "fourteen ";
case 5: return "fifteen ";
case 6: return "sixteen ";
case 7: return "seventeen ";
case 8: return "eighteen ";
case 9: return "nineteen ";
default: return "ten ";
}
}

private static String oneDig(int dig) {
switch (dig) {
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 "";
}
}