Integer#to_word

Convert an integer to the English reading of it. For example, 1"one".

Download

integer_to_word.rb.

License

GNU GPL.

Use

Tutorial

% irb -r integer_to_word.rb 
irb(main):001:0> 1.to_word
=> "one"
irb(main):002:0> 100.to_word
=> "one hundred"
irb(main):003:0> 126.to_word
=> "one hundred twenty six"
irb(main):004:0> 999999.to_word
=> "nine hundred ninety nine thousand nine hundred ninety nine"
irb(main):005:0> (1..10).each {|n| puts n.to_word}
one
two
three
four
five
six
seven
eight
nine
ten
=> 1..10

In Code

require 'integer_to_word'
class Range
  def ita_software_puzzle(n = 51000000000)
    t = n
    self.each do |i|
      w = i.to_word.gsub(' ','')
      w_l = w.length
      if t - w_l <= 0
        return [i, w[t].chr]
      else
        t -= w_l
      end
    end
    [nil,nil]
  end
end

puts (1..999999999).ita_software_puzzle

Requirements

Contact

Mike Burns mike@mike-burns.com