object IntegerToEnglishWords { val under20 = Array("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen") val tens = Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety") val thousands = Array("", "Thousand", "Million", "Billion") def numberToWords(num: Int): String = { if (num == 0) return "Zero" def convertLessThanThousand(n: Int): String = { if (n == 0) "" else if (n < 20) under20(n) + " " else if (n < 100) tens(n / 10) + " " + convertLessThanThousand(n % 10) else under20(n / 100) + " Hundred " + convertLessThanThousand(n % 100) } def helper(num: Int, index: Int): String = { if (num == 0) "" else if (num % 1000 != 0) convertLessThanThousand(num % 1000) + thousands(index) + " " + helper(num / 1000, index + 1) else helper(num / 1000, index + 1) } val words = helper(num, 0).trim words } def main(args: Array[String]): Unit = { val num1 = 12345 val num2 = 1234567 val num3 = 1234567890 println(s"$num1 in words: ${numberToWords(num1)}") println(s"$num2 in words: ${numberToWords(num2)}") println(s"$num3 in words: ${numberToWords(num3)}") } }