Class: NumbersInWords::ToWord
- Inherits:
-
Object
- Object
- NumbersInWords::ToWord
- Defined in:
- lib/numbers_in_words/to_word.rb
Instance Attribute Summary collapse
-
#that ⇒ Object
readonly
Returns the value of attribute that.
Instance Method Summary collapse
- #as_fraction(fraction) ⇒ Object
- #decimals ⇒ Object
- #handle_exceptional_numbers ⇒ Object
- #handle_tens(number) ⇒ Object
- #in_words(fraction: false) ⇒ Object
-
#initialize(that) ⇒ ToWord
constructor
A new instance of ToWord.
- #negative ⇒ Object
- #output ⇒ Object
- #to_i ⇒ Object
Constructor Details
#initialize(that) ⇒ ToWord
Returns a new instance of ToWord.
14 15 16 |
# File 'lib/numbers_in_words/to_word.rb', line 14 def initialize(that) @that = that end |
Instance Attribute Details
#that ⇒ Object (readonly)
Returns the value of attribute that.
12 13 14 |
# File 'lib/numbers_in_words/to_word.rb', line 12 def that @that end |
Instance Method Details
#as_fraction(fraction) ⇒ Object
36 37 38 |
# File 'lib/numbers_in_words/to_word.rb', line 36 def as_fraction(fraction) return Fraction.in_words(that) if fraction end |
#decimals ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/numbers_in_words/to_word.rb', line 40 def decimals int, decimals = NumberGroup.new(@that).split_decimals return unless int out = NumbersInWords.in_words(int) + ' point ' decimals.each do |decimal| out << NumbersInWords.in_words(decimal.to_i) + ' ' end out.strip end |
#handle_exceptional_numbers ⇒ Object
78 79 80 81 82 |
# File 'lib/numbers_in_words/to_word.rb', line 78 def handle_exceptional_numbers return unless @that.is_a?(Integer) NumbersInWords.exceptional_numbers.lookup(@that) end |
#handle_tens(number) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/numbers_in_words/to_word.rb', line 61 def handle_tens(number) output = '' tens = (number / 10).round * 10 # write the tens output += NumbersInWords.lookup(tens) # e.g. eighty digit = number - tens # write the digits unless digit.zero? join = number < 100 ? '-' : ' ' output << join + NumbersInWords.in_words(digit) end output end |
#in_words(fraction: false) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/numbers_in_words/to_word.rb', line 28 def in_words(fraction: false) as_fraction(fraction) || handle_exceptional_numbers || decimals || negative || output end |
#negative ⇒ Object
22 23 24 25 26 |
# File 'lib/numbers_in_words/to_word.rb', line 22 def negative return unless to_i.negative? 'minus ' + NumbersInWords.in_words(-@that) end |
#output ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/numbers_in_words/to_word.rb', line 51 def output output = if to_i.to_s.length == 2 # 20-99 handle_tens(to_i) else Writer.new(that).call # longer numbers end output.strip end |
#to_i ⇒ Object
18 19 20 |
# File 'lib/numbers_in_words/to_word.rb', line 18 def to_i that.to_i end |