Module: DateToWord

Defined in:
lib/date_to_word.rb

Class Method Summary collapse

Class Method Details

.date_to_words(date) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/date_to_word.rb', line 89

def self.date_to_words(date)
  d = date.strftime("%d-%B-%Y")
  d_arr = d.split("-")
  d_arr[0] = self.to_words(d_arr[0].to_i).capitalize
  d_arr[2] = self.years_to_words(d_arr[2].to_i).capitalize
  d_arr.join(" ")
end

.digitsObject



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/date_to_word.rb', line 56

def self.digits
  @digits ||= {
    0 => 'zero',
    1 => 'one',
    2 => 'two',
    3 => 'three',
    4 => 'four',
    5 => 'five',
    6 => 'six',
    7 => 'seven',
    8 => 'eight',
    9 => 'nine',
    10 => 'ten',
    11 => 'eleven',
    12 => 'twelve',
    13 => 'thirteen',
    14 => 'fourteen',
    15 => 'fifteen',
    16 => 'sixteen',
    17 => 'seventeen',
    18 => 'eighteen',
    19 => 'nineteen',
    20 => 'twenty',
    30 => 'thirty',
    40 => 'fourty',
    50 => 'fifty',
    60 => 'sixty',
    70 => 'seventy',
    80 => 'eighty',
    90 => 'ninety'
  }
end

.to_tens(u = nil, t = nil) ⇒ Object



52
53
54
# File 'lib/date_to_word.rb', line 52

def self.to_tens(u=nil, t=nil)
  "#{digits[t.to_i*10]} #{digits[u.to_i]}"
end

.to_units(u = nil, t = nil) ⇒ Object



47
48
49
50
# File 'lib/date_to_word.rb', line 47

def self.to_units(u=nil, t=nil)
  tens = "#{t}#{u}".to_i
  digits[tens]
end

.to_words(value) ⇒ 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
# File 'lib/date_to_word.rb', line 4

def self.to_words(value)
  value_length = value.to_s.split('.')
  value_r = value_length[0]
  value_p = value_length[1]
  a = []
  p = []
  words = []
  value_r.split('').each do |i|
    a << i
  end

  if value_p
    value_p.split('').each do |j|
      p << j
    end
  end

  a = a.reverse
  p = p.reverse
  result = to_units(a[0], a[1])
  result = to_tens(a[0], a[1]) unless result
  h_result = to_units(a[2])
  t_result = to_units(a[3], a[4])
  t_result = to_tens(a[3], a[4]) unless t_result
  l_result = to_units(a[5], a[6])
  l_result = to_tens(a[5], a[6]) unless l_result
  c_result = to_units(a[7], a[8])
  c_result = to_tens(a[7], a[8]) unless c_result
  p_result = to_units(p[0], p[1])
  p_result  = to_tens(p[0], p[1]) unless p_result

  # if you want to extend it beyond crore you can add your code here.

  words << "#{c_result} crore " if c_result && c_result != 'zero'
  words << "#{l_result} lakh " if l_result && l_result != 'zero'
  words << "#{t_result} thousand " if t_result && t_result != 'zero'
  words << "#{h_result} hundred " if h_result && h_result != 'zero'
  words << "#{result} " if result && result != 'zero'
  words << "and #{p_result} paise " if p_result && p_result != 'zero'
  # words << "only."
  return words.join
end

.years_to_words(y) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/date_to_word.rb', line 96

def self.years_to_words(y)
  year = y.to_s
  cents = year[0..1].to_s.to_i
  years = year[2..4].to_s.to_i

  cents_in_words = {
    19=>"Nineteen",
    20=>"Two Thousand ",
    21=>"Twenty one",
    22=>"Twenty two"

  }
  a  = {
    0=>"",
    1=>:one,
    2=>:two,
    3=>:three,
    4=>:four,
    5=>:five,
    6=>:six,
    7=>:seven,
    8=>:eight,
    9=>:nine,
    10=>:ten,
  }
  teens={
    10=>:ten,
    11=>:eleven,
    12=>:twleve,
    13=>:thirteen,
    14=>:fourteen,
    15=>:fifteen,
    16=>:sixteen,
    17=>:seventeen,
    18=>:eighteen,
    19=>:nineteen,
  }
  tens_words = {
    2=>:twenty,
    3=>:thirty,
    4=>:fourty,
    5=>:fifty,
    6=>:sixty,
    7=>:seventy,
    8=>:eighty,
    9=>:ninety,
  }

  hundreds = "#{cents_in_words[cents]}"

  if years == 0 && cents ==19
    tens =  "hundred"
  elsif years<10 && years >0 && cents ==19
    tens =  "hundred and #{a[years]}"
  elsif years<10 && years >0 && cents > 19
    tens = "and #{a[years]}"
  elsif years<20
    tens = "#{teens[years]}"
  else
    tens = "#{tens_words[years/10]}#{a[years%10]}"
  end

  year_in_words = "#{hundreds} #{tens}"

  return "#{year_in_words}"
end