Module: Nr2w

Defined in:
lib/nr2w.rb

Class Method Summary collapse

Class Method Details

.convert(value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nr2w.rb', line 17

def convert(value)
  deal_with_errors(value)

  # Transform into positive and set negative as true
  @negative =
    if value.negative?
      value = value.abs
      true
    end

  (0...value.digits.count).step(3) do |idx|
    hundreds_digits = value.digits.slice(idx, 3)
    number_notation(idx) if hundreds_digits.reverse.join.to_i.positive?
    unit_dozen_hundred(hundreds_digits)
  end

  @str.prepend('minus ') if @negative
  @str = 'zero' if value.zero?

  puts assemble_str(@str)
end

.deal_with_errors(value) ⇒ Object



39
40
41
42
# File 'lib/nr2w.rb', line 39

def deal_with_errors(value)
  raise 'Not an integer' unless value.is_a? Integer
  raise 'Overflow' unless value.abs < 2**100
end

.number_notation(index) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nr2w.rb', line 44

def number_notation(index)
  name =
    case index
    when 3
      ' thousand '
    when 6
      ' million '
    when 9
      ' billion '
    when 12
      ' trillion '
    when 15
      ' quadrillion '
    when 18
      ' quintillion '
    when 21
      ' sextillion '
    when 24
      ' septillion '
    when 27
      ' octillion '
    when 30
      ' nonillion '
    else
      ''
    end
  @str.prepend name
end

.unit_dozen_hundred(hundreds_digits) ⇒ Object

Units and dozens



9
10
11
12
13
14
15
# File 'lib/nr2w.rb', line 9

def unit_dozen_hundred(hundreds_digits)
  dozens = hundreds_digits.slice(0, 2).reverse.join.to_i
  @str.prepend dozens_str(dozens)
  return unless hundreds_digits.length > 2 && hundreds_digits[2]&.positive?

  @str.prepend hundreds_str(hundreds_digits[2], dozens.positive?)
end