numbers_in_words 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/numbers_in_words.rb +50 -77
- metadata +2 -2
data/lib/numbers_in_words.rb
CHANGED
@@ -9,6 +9,7 @@ module NumbersToWords
|
|
9
9
|
20 => "twenty", 30=>"thirty",
|
10
10
|
40=>"forty", 50=>"fifty", 60 => "sixty", 70=> "seventy", 80=>"eighty",
|
11
11
|
90 => "ninety"}
|
12
|
+
DIGITS= %w[zero one two three four five six seven eight nine]
|
12
13
|
POWERS_OF_TEN ={0=>"one", 1 => "ten", 2=> "hundred",
|
13
14
|
3 => "thousand", 6=>"million",
|
14
15
|
9=>"billion",
|
@@ -54,12 +55,6 @@ module NumbersToWords
|
|
54
55
|
end
|
55
56
|
end
|
56
57
|
|
57
|
-
JAPANESE_POWERS_OF_TEN ={0=>"一", 1 => "十", 2=> "百",
|
58
|
-
3 => "千", 4=>"万",8=>"億", 12=>"兆"}
|
59
|
-
|
60
|
-
DIGITS= %w[zero one two three four five six seven eight nine]
|
61
|
-
JAPANESE_DIGITS= %w[〇 一 二 三 四 五 六 七 八 九]
|
62
|
-
|
63
58
|
#returns a hash with powers of ten and their multipliers
|
64
59
|
def powers_of_ten
|
65
60
|
i = self.to_i
|
@@ -114,13 +109,54 @@ module NumbersToWords
|
|
114
109
|
powers = groups.keys.sort.reverse #put in descending order
|
115
110
|
powers.each do |power|
|
116
111
|
name = POWERS_OF_TEN[power] if language=="English"
|
117
|
-
name = JAPANESE_POWERS_OF_TEN[power] if language=="Japanese"
|
118
112
|
digits = groups[power]
|
119
113
|
yield power, name, digits
|
120
114
|
end
|
121
115
|
|
122
116
|
end
|
123
117
|
|
118
|
+
def english_group group_size
|
119
|
+
number = self.to_i
|
120
|
+
output = ""
|
121
|
+
number.group_words(group_size) do |power, name, digits|
|
122
|
+
if digits > 0
|
123
|
+
prefix = " "
|
124
|
+
#no 'and' between thousands and hundreds
|
125
|
+
prefix << "and " if power == 0 and digits < 100
|
126
|
+
output << prefix + digits.in_english
|
127
|
+
output << prefix + name unless power == 0
|
128
|
+
end
|
129
|
+
end
|
130
|
+
return output
|
131
|
+
end
|
132
|
+
|
133
|
+
def split_googols
|
134
|
+
number = self.to_i
|
135
|
+
output = ""
|
136
|
+
googols = number.to_s[0..(-LENGTH_OF_GOOGOL)].to_i
|
137
|
+
remainder = number.to_s[1-LENGTH_OF_GOOGOL .. -1].to_i
|
138
|
+
output << " " + googols.in_words + " googol"
|
139
|
+
if remainder > 0
|
140
|
+
prefix = " "
|
141
|
+
prefix << "and " if remainder < 100
|
142
|
+
output << prefix + remainder.in_english
|
143
|
+
end
|
144
|
+
return output
|
145
|
+
end
|
146
|
+
|
147
|
+
def in_googols
|
148
|
+
|
149
|
+
number = self.to_i
|
150
|
+
output = ""
|
151
|
+
output << " " + number.to_s[0..0].to_i.in_english + " googol"
|
152
|
+
remainder = number.to_s[1..-1].to_i
|
153
|
+
prefix = " "
|
154
|
+
prefix << "and " if remainder < 100
|
155
|
+
output << prefix + remainder.in_english if remainder > 0
|
156
|
+
|
157
|
+
return output
|
158
|
+
end
|
159
|
+
|
124
160
|
def in_english
|
125
161
|
number = self.to_i # make a copy
|
126
162
|
#handle 0-10
|
@@ -133,95 +169,32 @@ module NumbersToWords
|
|
133
169
|
tens = (number/10).round*10 #write the tens
|
134
170
|
# e.g. eighty
|
135
171
|
output << EXCEPTIONS[tens]
|
136
|
-
|
137
172
|
#write the digits
|
138
173
|
digit= number - tens
|
139
174
|
output << " " + digit.in_english unless digit==0
|
140
|
-
|
141
175
|
elsif length == 3
|
142
176
|
#e.g. 113 splits into "one hundred" and "thirteen"
|
143
|
-
number.
|
144
|
-
|
145
|
-
|
146
|
-
#no and between thousands and hundreds
|
147
|
-
prefix << "and " if power == 0
|
148
|
-
output << prefix + digits.in_english
|
149
|
-
output << prefix + name if power == 2 #add the hundred
|
150
|
-
end
|
151
|
-
end
|
152
|
-
elsif length < 101 #more than one hundred less than one googol
|
153
|
-
number.group_words(3) do |power, name, digits|
|
154
|
-
if digits > 0
|
155
|
-
prefix = " "
|
156
|
-
prefix << "and " if power==0 and digits < 100
|
157
|
-
output << prefix + digits.in_english
|
158
|
-
output << " " + name unless power==0
|
159
|
-
end
|
160
|
-
end
|
161
|
-
elsif length > LENGTH_OF_GOOGOL #one googol and larger
|
162
|
-
googols = number.to_s[0..(-LENGTH_OF_GOOGOL)].to_i
|
163
|
-
remainder = number.to_s[1-LENGTH_OF_GOOGOL .. -1].to_i
|
164
|
-
output << " " + googols.in_english + " googol"
|
165
|
-
if remainder > 0
|
166
|
-
prefix = " "
|
167
|
-
prefix << "and " if remainder < 100
|
168
|
-
output << prefix + remainder.in_english
|
169
|
-
end
|
177
|
+
output << number.english_group(2)
|
178
|
+
elsif length < LENGTH_OF_GOOGOL #more than one hundred less than one googol
|
179
|
+
output << number.english_group(3)
|
170
180
|
elsif length == LENGTH_OF_GOOGOL
|
171
|
-
output <<
|
172
|
-
|
173
|
-
|
174
|
-
prefix << "and " if remainder < 100
|
175
|
-
output << prefix + remainder.in_english if remainder > 0
|
181
|
+
output << number.in_googols
|
182
|
+
elsif length > LENGTH_OF_GOOGOL #one googol and larger
|
183
|
+
output << number.split_googols
|
176
184
|
end
|
177
185
|
|
178
186
|
return output.strip
|
179
187
|
end
|
180
188
|
|
181
|
-
def in_japanese
|
182
|
-
number = self.to_i # make a copy
|
183
|
-
#handle 0-10
|
184
|
-
return DIGITS[number] if number < 10
|
185
|
-
return EXCEPTIONS[number] if EXCEPTIONS[number]
|
186
|
-
|
187
|
-
output = ""
|
188
|
-
if number.to_s.length == 2 #20-99
|
189
|
-
|
190
|
-
#write the tens
|
191
|
-
tens = (number/10).round
|
192
|
-
# e.g. eighty
|
193
|
-
output << JAPANESE_DIGITS[tens] << JAPANESE_POWERS_OF_TEN[1]
|
194
|
-
digit= number - tens
|
195
|
-
output << " " << JAPANESE_DIGITS[digit]
|
196
|
-
#write the digits
|
197
|
-
else #more than one hundred
|
198
|
-
number.group_words 3 do |power, name, digits|
|
199
|
-
if digits > 0
|
200
|
-
prefix = " "
|
201
|
-
prefix << "and " if power==0 and digits < 100
|
202
|
-
output << prefix + digits.in_english
|
203
|
-
output << " " + name unless power==0
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
return output.strip
|
208
|
-
|
209
|
-
end
|
210
|
-
|
211
189
|
def in_words language="English"
|
212
190
|
case language
|
213
191
|
when "English" #allow for I18n
|
214
192
|
in_english
|
215
|
-
when "Japanese"
|
216
|
-
in_japanese
|
217
193
|
end
|
218
194
|
end
|
219
195
|
end
|
220
196
|
|
221
|
-
class
|
197
|
+
class Numeric
|
222
198
|
include NumbersToWords
|
223
199
|
end
|
224
|
-
class Bignum
|
225
200
|
|
226
|
-
include NumbersToWords
|
227
|
-
end
|