Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/random-words/number_to_word.rb

Overview

This class extends the Integer class with methods to convert numbers into their word representations. It provides methods for converting numbers to words in various languages, including English.

Examples:

1.to_word # => "one"
126620.to_word # => "one hundred twenty six thousand six hundred twenty"

Instance Method Summary collapse

Instance Method Details

#additional_tests(numbers) ⇒ Object

For testing edge cases



47
48
49
50
51
52
# File 'lib/random-words/number_to_word.rb', line 47

def additional_tests(numbers)
  teen_to_word(numbers)
  digit_to_word(numbers)
  tens_place_to_word(numbers)
  true
end

#to_word(numbers) ⇒ Object

Turn a number into the word representation of that number.

1.to_word      # => "one"
126620.to_word # => "one hundred twenty six thousand six hundred twenty"


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/random-words/number_to_word.rb', line 34

def to_word(numbers)
  tmp = self / 1000
  final = (self % 1000).hundred_to_word(numbers, 2)
  place = 3 # special-case the tens and below
  until tmp.zero?
    final = "#{(tmp % 1000).hundred_to_word(numbers, place)} #{final}"
    place += 1
    tmp /= 1000
  end
  final == '' ? 'zero' : final.sub(/ and *$/, '').sub(/\s+$/, '')
end