numbers_in_words 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/numbers_in_words.rb +11 -3
- data/spec/numbers_in_words_spec.rb +15 -0
- metadata +4 -4
data/lib/numbers_in_words.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'active_support'
|
3
|
-
module
|
3
|
+
module NumbersInWords
|
4
4
|
|
5
5
|
#handle exceptions to normal numbers
|
6
6
|
EXCEPTIONS = {10=> "ten", 11=>"eleven", 12 => "twelve", 13 => "thirteen",
|
@@ -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
|
+
|
12
13
|
DIGITS= %w[zero one two three four five six seven eight nine]
|
13
14
|
POWERS_OF_TEN ={0=>"one", 1 => "ten", 2=> "hundred",
|
14
15
|
3 => "thousand", 6=>"million",
|
@@ -159,6 +160,10 @@ module NumbersToWords
|
|
159
160
|
|
160
161
|
def in_english
|
161
162
|
number = self.to_i # make a copy
|
163
|
+
#handle negative numbers
|
164
|
+
if number < 0
|
165
|
+
return "minus " + (-number).in_english
|
166
|
+
end
|
162
167
|
#handle 0-10
|
163
168
|
return DIGITS[number] if number < 10
|
164
169
|
return EXCEPTIONS[number] if EXCEPTIONS[number]
|
@@ -194,7 +199,10 @@ module NumbersToWords
|
|
194
199
|
end
|
195
200
|
end
|
196
201
|
|
197
|
-
class
|
198
|
-
include
|
202
|
+
class Fixnum
|
203
|
+
include NumbersInWords
|
199
204
|
end
|
200
205
|
|
206
|
+
class Bignum
|
207
|
+
include NumbersInWords
|
208
|
+
end
|
@@ -28,6 +28,21 @@ describe Fixnum do
|
|
28
28
|
123456.powers_of_ten.should == {0=>6,1=>5,2=>4,3=>3,4=>2,5=>1}
|
29
29
|
end
|
30
30
|
|
31
|
+
it "should handle negative numbers" do
|
32
|
+
-1.in_words.should == "minus one"
|
33
|
+
-9.in_words.should == "minus nine"
|
34
|
+
-10.in_words.should == "minus ten"
|
35
|
+
-15.in_words.should == "minus fifteen"
|
36
|
+
-100.in_words.should == "minus one hundred"
|
37
|
+
(-1*(10**100)).in_words.should == "minus one googol"
|
38
|
+
-123456789.in_words.should == "minus one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine"
|
39
|
+
end
|
40
|
+
it "should handle decimals" do
|
41
|
+
1.1.in_words.should == "one point one"
|
42
|
+
1.2345678.in_words.should == "one point two three four five six seven eight"
|
43
|
+
1000.2345678.in_words.should == "one thousand point two three four five six seven eight"
|
44
|
+
12345.2345678.in_words.should == "twelve thousand three hundred and forty five point two three four five six seven eight"
|
45
|
+
end
|
31
46
|
it "should split into group of three digit numbers" do
|
32
47
|
1.groups_of(3).should == {0=>1}
|
33
48
|
12.groups_of(3).should == {0=>12}
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 5
|
9
|
+
version: 0.0.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Mark Burns
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-05 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description: "Use the #in_words method on a Fixnum or Bignum to try it out
|
21
|
+
description: "Use the #in_words method on a positive or negative Fixnum or Bignum to try it out"
|
22
22
|
email: markthedeveloper@googlemail.com
|
23
23
|
executables: []
|
24
24
|
|