numbers_in_words 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/display_numbers_in_words.rb +22 -0
- data/spec/numbers_in_words_spec.rb +88 -0
- metadata +4 -2
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'numbers_in_words'
|
3
|
+
def usage code=-1
|
4
|
+
puts "\nUsage:\n\nruby #{__FILE__} <start> <end>"
|
5
|
+
exit code
|
6
|
+
end
|
7
|
+
if ARGV.length != 2
|
8
|
+
|
9
|
+
puts "Start and end arguments expected"
|
10
|
+
usage -1
|
11
|
+
end
|
12
|
+
start_number = ARGV[0].to_i
|
13
|
+
end_number = ARGV[1].to_i
|
14
|
+
|
15
|
+
if start_number > end_number
|
16
|
+
puts "Start number must be less than or equal to the end number"
|
17
|
+
usage -2
|
18
|
+
end
|
19
|
+
|
20
|
+
for i in start_number..end_number do
|
21
|
+
puts i.in_words
|
22
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'lib/numbers_in_words'
|
2
|
+
describe Fixnum do
|
3
|
+
it "should print the digits 0-9 correctly" do
|
4
|
+
numbers = %w[zero one two three four five six seven eight nine ten]
|
5
|
+
10.times do |i|
|
6
|
+
puts i.in_words
|
7
|
+
i.in_words.should==numbers[i]
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
it "should print the digits 11-19 correctly" do
|
12
|
+
words = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen]
|
13
|
+
numbers = [11,12,13,14,15,16,17,18,19]
|
14
|
+
numbers.each_with_index do |number, index|
|
15
|
+
puts number.in_words
|
16
|
+
number.in_words.should==words[index]
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return a hash of powers of ten" do
|
22
|
+
117.powers_of_ten.should == {0=>7,1=>1,2=>1}
|
23
|
+
0.powers_of_ten.should == {}
|
24
|
+
1.powers_of_ten.should == {0=>1}
|
25
|
+
9.powers_of_ten.should == {0=>9}
|
26
|
+
10.powers_of_ten.should == {1=>1}
|
27
|
+
18.powers_of_ten.should == {1=>1,0=>8}
|
28
|
+
123456.powers_of_ten.should == {0=>6,1=>5,2=>4,3=>3,4=>2,5=>1}
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should split into group of three digit numbers" do
|
32
|
+
1.groups_of(3).should == {0=>1}
|
33
|
+
12.groups_of(3).should == {0=>12}
|
34
|
+
123.groups_of(3).should == {0=>123}
|
35
|
+
1111.groups_of(3).should == {3=>1,0=>111}
|
36
|
+
87654.groups_of(3).should == {3=>87,0=>654}
|
37
|
+
1234567.groups_of(3).should == {6=>1,3=>234,0=>567}
|
38
|
+
123456789101112.groups_of(3).should == {12=>123,9=>456,6=>789,3=>101,0=>112}
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should display numbers grouped" do
|
42
|
+
2111.group_words 3 do |power, name, digits|
|
43
|
+
puts "10^#{power} #{name} #{ digits }"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should handle two digit numbers" do
|
48
|
+
21.in_words.should == "twenty one"
|
49
|
+
end
|
50
|
+
it "should handle three digit numbers" do
|
51
|
+
113.in_words.should == "one hundred and thirteen"
|
52
|
+
299.in_words.should == "two hundred and ninety nine"
|
53
|
+
300.in_words.should == "three hundred"
|
54
|
+
101.in_words.should == "one hundred and one"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should print out some random examples correctly" do
|
58
|
+
2999.in_words.should == "two thousand nine hundred and ninety nine"
|
59
|
+
99999.in_words.should == "ninety nine thousand nine hundred and ninety nine"
|
60
|
+
999999.in_words.should == "nine hundred and ninety nine thousand nine hundred and ninety nine"
|
61
|
+
123456.in_words.should == "one hundred and twenty three thousand four hundred and fifty six"
|
62
|
+
17054.in_words.should == "seventeen thousand and fifty four"
|
63
|
+
11004.in_words.should == "eleven thousand and four"
|
64
|
+
470154.in_words.should == "four hundred and seventy thousand one hundred and fifty four"
|
65
|
+
417155.in_words.should == "four hundred and seventeen thousand one hundred and fifty five"
|
66
|
+
999999.in_words.should == "nine hundred and ninety nine thousand nine hundred and ninety nine"
|
67
|
+
1000000.in_words.should == "one million"
|
68
|
+
1000001.in_words.should == "one million and one"
|
69
|
+
112.in_words.should == "one hundred and twelve"
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should handle edge cases" do
|
74
|
+
1000001.in_words.should == "one million and one"
|
75
|
+
(10*10**12 + 10**6 +1).in_words.should == "ten trillion one million and one"
|
76
|
+
(10**75).in_words.should == "one quattuorvigintillion"
|
77
|
+
10001001.in_words.should == "ten million one thousand and one"
|
78
|
+
end
|
79
|
+
it "should handle a googol and larger" do
|
80
|
+
n=10**100
|
81
|
+
puts n.in_words
|
82
|
+
(10**100 + 1).in_words.should == "one googol and one"
|
83
|
+
(42*10**100 + 16777216).in_words.should == "forty two googol sixteen million seven hundred and seventy seven thousand two hundred and sixteen"
|
84
|
+
(42* 10**100 * 10**100).in_words.should == "forty two googol googol"
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
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
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Mark Burns
|
@@ -29,6 +29,8 @@ extra_rdoc_files: []
|
|
29
29
|
files:
|
30
30
|
- README
|
31
31
|
- lib/numbers_in_words.rb
|
32
|
+
- examples/display_numbers_in_words.rb
|
33
|
+
- spec/numbers_in_words_spec.rb
|
32
34
|
has_rdoc: true
|
33
35
|
homepage: http://github.com/markburns/numbers_in_words
|
34
36
|
licenses: []
|