Dan Schmidt <dfan / harmonixmusic.com> wrote: >David Alan Black <dblack / candle.superlink.net> writes: > >| Input: 123456 >| Output: "one-hundred twenty-three thousand four-hundred fifty-six" > >I don't like hyphens, but it would be easy enough to put them in: > >def english_num (num) > ones_names = '.one.two.three.four.five.six.seven.eight.nine.ten.eleven.twelve.thirteen.fourteen.fifteen.sixteen.seventeen.eighteen.nineteen'.split('.') Nice way to set up an array of strings. I'll borrow it. It beats ['', 'one', 'two'...]. Also, my attempt had separate arrays for ones and teens. This makes more sense. > tens_names = '.ten.twenty.thirty.forty.fifty.sixty.seventy.eighty.ninety'.split('.') > > ['', 'thousand', 'million', 'billion', 'trillion'].map { |gstr| > (num, group) = num.divmod (1000) Interesting. My code used 'group' to mean thousands, millions, etc. Yours seems to use it to mean hundreds-tens-ones. > str = "" > if group > 0 > (hundreds, rest) = group.divmod (100) > str << "#{ones_names[hundreds]} hundred " if hundreds > 0 > if rest < 20 > str << ones_names[rest] > else > (tens, ones) = rest.divmod(10) > str << "#{tens_names[tens]} #{ones_names[ones]}" > end > str << " #{gstr}" > end Lots of Ruby tricks in here. Harder for a nuby to understand, but very compact. For now, at least, I like the look of my code better. Question: How hard would it be for this code to return 'zero' for 0? > str > }.reverse.join(' ').squeeze(' ').strip Hmmmmmm. This line sounds...pornographic. :-) >end > >print english_num (ARGV[0].to_i), "\n" Kevin