numbers_in_words 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +13 -1
- data/lib/numbers_in_words.rb +69 -14
- metadata +12 -5
data/README
CHANGED
@@ -1,8 +1,20 @@
|
|
1
|
+
Installation
|
2
|
+
============
|
3
|
+
|
4
|
+
|
5
|
+
gem install numbers_in_words
|
6
|
+
|
7
|
+
or
|
8
|
+
|
9
|
+
sudo gem install numbers_in_words
|
10
|
+
|
11
|
+
|
12
|
+
|
1
13
|
The file numbers_to_words defines a module NumbersToWords which is included in Fixnum and Bignum.
|
2
14
|
The in_words method can then be used on any Fixnum or Bignum object.
|
3
15
|
|
4
16
|
E.g.
|
5
|
-
|
17
|
+
>require 'numbers_in_words'
|
6
18
|
>112.in_words
|
7
19
|
#=> one hundred and twelve
|
8
20
|
|
data/lib/numbers_in_words.rb
CHANGED
@@ -9,13 +9,54 @@ 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
|
-
|
13
12
|
POWERS_OF_TEN ={0=>"one", 1 => "ten", 2=> "hundred",
|
14
|
-
3 => "thousand", 6=>"million",
|
13
|
+
3 => "thousand", 6=>"million",
|
14
|
+
9=>"billion",
|
15
|
+
12=>"trillion",
|
16
|
+
15=>"quadrillion",
|
17
|
+
18=>"quintillion",
|
18
|
+
21=>"sextillion",
|
19
|
+
24=>"septillion",
|
20
|
+
27=>"octillion",
|
21
|
+
30=>"nonillion",
|
22
|
+
33=>"decillion",
|
23
|
+
36=>"undecillion",
|
24
|
+
39=>"duodecillion",
|
25
|
+
42=>"tredecillion",
|
26
|
+
45=>"quattuordecillion",
|
27
|
+
48=>"quindecillion",
|
28
|
+
51=>"sexdecillion",
|
29
|
+
54=>"septendecillion",
|
30
|
+
57=>"octodecillion",
|
31
|
+
60=>"novemdecillion",
|
32
|
+
63=>"vigintillion",
|
33
|
+
66=>"unvigintillion",
|
34
|
+
69=>"duovigintillion",
|
35
|
+
72=>"trevigintillion",
|
36
|
+
75=>"quattuorvigintillion",
|
37
|
+
78=>"quinvigintillion",
|
38
|
+
81=>"sexvigintillion",
|
39
|
+
84=>"septenvigintillion",
|
40
|
+
87=>"octovigintillion",
|
41
|
+
90=>"novemvigintillion",
|
42
|
+
93=>"trigintillion",
|
43
|
+
96=>"untrigintillion",
|
44
|
+
99=>"duotrigintillion",
|
45
|
+
100 => "googol"
|
46
|
+
}
|
47
|
+
LENGTH_OF_GOOGOL = 101 #length of the string i.e. one with 100 zeros
|
48
|
+
|
49
|
+
def initialize
|
50
|
+
power = 9
|
51
|
+
POWERS_OF_TEN_NAMES.each do |name|
|
52
|
+
POWERS_OF_TEN[power]=name
|
53
|
+
power += 3
|
54
|
+
end
|
55
|
+
end
|
15
56
|
|
16
57
|
JAPANESE_POWERS_OF_TEN ={0=>"一", 1 => "十", 2=> "百",
|
17
58
|
3 => "千", 4=>"万",8=>"億", 12=>"兆"}
|
18
|
-
|
59
|
+
|
19
60
|
DIGITS= %w[zero one two three four five six seven eight nine]
|
20
61
|
JAPANESE_DIGITS= %w[〇 一 二 三 四 五 六 七 八 九]
|
21
62
|
|
@@ -25,7 +66,7 @@ module NumbersToWords
|
|
25
66
|
digits=i.to_s.split ""
|
26
67
|
#turn back into integers
|
27
68
|
digits.map! { |x| x.to_i}
|
28
|
-
|
69
|
+
|
29
70
|
digits.reverse!
|
30
71
|
#create a hash where the key is the
|
31
72
|
#power of ten and the value is the multipler
|
@@ -54,7 +95,7 @@ module NumbersToWords
|
|
54
95
|
#turn back into integers
|
55
96
|
groups.map! {|group| group.join("").to_i }
|
56
97
|
groups.reverse! # put in ascending order of power of ten
|
57
|
-
|
98
|
+
|
58
99
|
#output hash where key is the power of ten
|
59
100
|
#and value if the multiplier
|
60
101
|
power = 0
|
@@ -87,10 +128,9 @@ module NumbersToWords
|
|
87
128
|
return EXCEPTIONS[number] if EXCEPTIONS[number]
|
88
129
|
|
89
130
|
output = ""
|
90
|
-
|
91
|
-
|
92
|
-
#write the tens
|
93
|
-
tens = (number/10).round*10
|
131
|
+
length = number.to_s.length
|
132
|
+
if length == 2 #20-99
|
133
|
+
tens = (number/10).round*10 #write the tens
|
94
134
|
# e.g. eighty
|
95
135
|
output << EXCEPTIONS[tens]
|
96
136
|
|
@@ -98,7 +138,7 @@ module NumbersToWords
|
|
98
138
|
digit= number - tens
|
99
139
|
output << " " + digit.in_english unless digit==0
|
100
140
|
|
101
|
-
elsif
|
141
|
+
elsif length == 3
|
102
142
|
#e.g. 113 splits into "one hundred" and "thirteen"
|
103
143
|
number.group_words(2) do |power, name, digits|
|
104
144
|
if digits > 0
|
@@ -109,9 +149,8 @@ module NumbersToWords
|
|
109
149
|
output << prefix + name if power == 2 #add the hundred
|
110
150
|
end
|
111
151
|
end
|
112
|
-
|
113
|
-
|
114
|
-
number.group_words 3 do |power, name, digits|
|
152
|
+
elsif length < 101 #more than one hundred less than one googol
|
153
|
+
number.group_words(3) do |power, name, digits|
|
115
154
|
if digits > 0
|
116
155
|
prefix = " "
|
117
156
|
prefix << "and " if power==0 and digits < 100
|
@@ -119,7 +158,23 @@ module NumbersToWords
|
|
119
158
|
output << " " + name unless power==0
|
120
159
|
end
|
121
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
|
170
|
+
elsif length == LENGTH_OF_GOOGOL
|
171
|
+
output << " " + number.to_s[0..0].to_i.in_english + " googol"
|
172
|
+
remainder = number.to_s[1..-1].to_i
|
173
|
+
prefix = " "
|
174
|
+
prefix << "and " if remainder < 100
|
175
|
+
output << prefix + remainder.in_english if remainder > 0
|
122
176
|
end
|
177
|
+
|
123
178
|
return output.strip
|
124
179
|
end
|
125
180
|
|
@@ -150,7 +205,7 @@ module NumbersToWords
|
|
150
205
|
end
|
151
206
|
end
|
152
207
|
return output.strip
|
153
|
-
|
208
|
+
|
154
209
|
end
|
155
210
|
|
156
211
|
def in_words language="English"
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: numbers_in_words
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Mark Burns
|
@@ -37,18 +42,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
42
|
requirements:
|
38
43
|
- - ">="
|
39
44
|
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
40
47
|
version: "0"
|
41
|
-
version:
|
42
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
49
|
requirements:
|
44
50
|
- - ">="
|
45
51
|
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
46
54
|
version: "0"
|
47
|
-
version:
|
48
55
|
requirements: []
|
49
56
|
|
50
|
-
rubyforge_project:
|
51
|
-
rubygems_version: 1.3.
|
57
|
+
rubyforge_project: numbers_in_words
|
58
|
+
rubygems_version: 1.3.6
|
52
59
|
signing_key:
|
53
60
|
specification_version: 3
|
54
61
|
summary: Turns integers into their long-hand text form like "two thousand and one"
|