fork download
  1.  
  2. class Integer
  3. def to_english
  4. return to_english_under_1000 if self < 1000
  5. unit = %w[
  6. \ thousand million billion trillion quadrillion quintillion
  7. ]
  8. to_s.reverse.scan(/.{1,3}/).zip(unit).reduce('') do |s, (d, u)|
  9. d =~ /\A0+\z/ ? s : [d.reverse.to_i.to_english_under_1000, u, s] * ' '
  10. end.strip
  11. end
  12.  
  13. def to_english_under_1000
  14. a, b = divmod(100)
  15. s = b.to_english_under_100
  16. return s if self < 100
  17. a.to_english_under_20 + ' hundred' + (b.zero? ? '' : ' ' + s)
  18. end
  19.  
  20. def to_english_under_100
  21. return to_english_under_20 if self < 20
  22. a, b = divmod(10)
  23. d = %w[
  24. \ \ twenty thirty forty fifty sixty seventy eighty ninety
  25. ][a]
  26. b.zero? ? d : d + ' ' + b.to_english_under_20
  27. end
  28.  
  29. def to_english_under_20
  30. %w[
  31. zero one two three four five six seven eight nine ten eleven
  32. twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
  33. ][self]
  34. end
  35. end
  36.  
  37. puts $_.to_i.to_english while gets
  38.  
Success #stdin #stdout 0.01s 5868KB
stdin
0
134
1000000
4294967295
18446744073709551616
stdout
zero
one hundred thirty four
one million
four billion two hundred ninety four million nine hundred sixty seven thousand two hundred ninety five
eighteen quintillion four hundred forty six quadrillion seven hundred forty four trillion seventy three billion seven hundred nine million five hundred fifty one thousand six hundred sixteen