Module: Numstr

Defined in:
lib/numstr.rb,
lib/numstr/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.to_str(num) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/numstr.rb', line 4

def self.to_str(num)
  # return "C'mon! '#{num}' is not a number!" unless num.is_a? Integer
  return 'zero, null, nil, nada, zip, goose egg' if num.zero?

  if num.negative?
    negative = true
    num = num.abs
  end

  num_string = ''

  ones_array = %w[one two three four five six seven eight nine]
  tens_array = %w[ten twenty thirty forty fifty sixty seventy eighty ninety]
  teenagers_array = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen]

  zillions = [
    ['hundred', 2],
    ['thousand', 3],
    ['million', 6],
    ['billion', 9],
    ['trillion', 12],
    ['quadrillion', 15],
    ['quintillion', 18],
    ['sextillion', 21],
    ['septillion', 24],
    ['octillion', 27],
    ['nonillion', 30],
    ['decillion', 33],
    ['undecillion', 36],
    ['duodecillion', 39],
    ['tredecillion', 42],
    ['quattuordecillion', 45],
    ['quindecillion', 48],
    ['sexdecillion', 51],
    ['septendecillion', 54],
    ['octodecillion', 57],
    ['novemdecillion', 60],
    ['vigintillion', 63],
    ['googol', 100]
  ]

  left = num

  if negative
    num_string = 'negative' + ' '
  end

  until zillions.empty?
    zill = zillions.pop
    zill_name = zill[0]
    zill_base = 10**zill[1]

    write = left / zill_base
    left -= write * zill_base

    if write > 0
      prefix = to_str write
      num_string += prefix + ' ' + zill_name

      num_string += ' ' if left.positive?
    end
  end

  write = left / 10
  left -= write * 10

  if write > 0
    if write == 1 && left > 0
      num_string += teenagers_array[left -1]
      left = 0
    else
      num_string += tens_array[write - 1]
    end
    num_string += ' ' if left.positive?
  end

  write = left
  left = 0

  if write > 0
    num_string += ones_array[write - 1]
  end
  num_string
end