public class NumberToWords {
private static final String[] BELOW_20 = {
"", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen"
};
private static final String[] TENS = {
"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
};
private static String setBelow20AndTens(int num) {
if (num == 0) return "";
if (num < 20) {
return BELOW_20[num] + " ";
} else if (num < 100) {
return TENS[num / 10] + " " + setBelow20AndTens(num % 10);
} else {
return BELOW_20[num / 100] + " hundred " + setBelow20AndTens(num % 100);
}
}
public static String numberToWords(int num) {
if (num == 0) return "zero";
StringBuilder result = new StringBuilder();
if (num >= 1_000_000_000) {
result.append(setBelow20AndTens(num / 1_000_000_000)).append("billion ");
num %= 1_000_000_000;
}
if (num >= 1_000_000) {
result.append(setBelow20AndTens(num / 1_000_000)).append("million ");
num %= 1_000_000;
}
if (num >= 1000) {
result.append(setBelow20AndTens(num / 1000)).append("thousand ");
num %= 1000;
}
if (num > 0) {
result.append(setBelow20AndTens(num));
}
return result.toString().trim();
}
public static void main(String[] args) {
int n = 176283;
System.out.println(numberToWords(n));
}
}
/*
run:
one hundred seventy six thousand two hundred eighty three
*/