numbertotext-ruby 1.0.0
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.
- checksums.yaml +7 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +68 -0
- data/Guardfile +5 -0
- data/README.md +24 -0
- data/lib/numbertotext/version.rb +3 -0
- data/lib/numbertotext.rb +48 -0
- data/numbertotext.gemspec +19 -0
- data/spec/lib/numbertotext_spec.rb +152 -0
- data/spec/spec_helper.rb +12 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 648a8f9b8f448c41208dd6d36fc8f5bfb8091a3e
|
4
|
+
data.tar.gz: eb8736bdf21bee2b440ca7b437e17d2543e631a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 35e4b388284ee17913ea084bcf03254569b04cabb1876f1d84c0860b1b48588b3045cb268de6d76d3e6aa837556c655f13dd3db82c8b0593a7ccb65cdecaa7c8
|
7
|
+
data.tar.gz: abd1ad9003ae0ca4d7503ee9b79aae0145edfd5f40abf1f6a787ff4045d97a6ad1f5db1d61aa2b97437e56edb8abbc7b8d2613e6d3bbc0aafaaedb0c05f6515f
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
celluloid (0.16.0)
|
5
|
+
timers (~> 4.0.0)
|
6
|
+
coderay (1.1.0)
|
7
|
+
diff-lcs (1.2.5)
|
8
|
+
ffi (1.9.8)
|
9
|
+
formatador (0.2.5)
|
10
|
+
guard (2.12.5)
|
11
|
+
formatador (>= 0.2.4)
|
12
|
+
listen (~> 2.7)
|
13
|
+
lumberjack (~> 1.0)
|
14
|
+
nenv (~> 0.1)
|
15
|
+
notiffany (~> 0.0)
|
16
|
+
pry (>= 0.9.12)
|
17
|
+
shellany (~> 0.0)
|
18
|
+
thor (>= 0.18.1)
|
19
|
+
guard-compat (1.2.1)
|
20
|
+
guard-rspec (4.5.1)
|
21
|
+
guard (~> 2.1)
|
22
|
+
guard-compat (~> 1.1)
|
23
|
+
rspec (>= 2.99.0, < 4.0)
|
24
|
+
hitimes (1.2.2)
|
25
|
+
listen (2.10.0)
|
26
|
+
celluloid (~> 0.16.0)
|
27
|
+
rb-fsevent (>= 0.9.3)
|
28
|
+
rb-inotify (>= 0.9)
|
29
|
+
lumberjack (1.0.9)
|
30
|
+
method_source (0.8.2)
|
31
|
+
nenv (0.2.0)
|
32
|
+
notiffany (0.0.6)
|
33
|
+
nenv (~> 0.1)
|
34
|
+
shellany (~> 0.0)
|
35
|
+
pry (0.10.1)
|
36
|
+
coderay (~> 1.1.0)
|
37
|
+
method_source (~> 0.8.1)
|
38
|
+
slop (~> 3.4)
|
39
|
+
rake (10.4.2)
|
40
|
+
rb-fsevent (0.9.5)
|
41
|
+
rb-inotify (0.9.5)
|
42
|
+
ffi (>= 0.5.0)
|
43
|
+
rspec (3.3.0)
|
44
|
+
rspec-core (~> 3.3.0)
|
45
|
+
rspec-expectations (~> 3.3.0)
|
46
|
+
rspec-mocks (~> 3.3.0)
|
47
|
+
rspec-core (3.3.2)
|
48
|
+
rspec-support (~> 3.3.0)
|
49
|
+
rspec-expectations (3.3.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.3.0)
|
52
|
+
rspec-mocks (3.3.2)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.3.0)
|
55
|
+
rspec-support (3.3.0)
|
56
|
+
shellany (0.0.1)
|
57
|
+
slop (3.6.0)
|
58
|
+
thor (0.19.1)
|
59
|
+
timers (4.0.1)
|
60
|
+
hitimes
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
ruby
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
guard-rspec
|
67
|
+
rake (~> 10.1)
|
68
|
+
rspec (~> 3.0)
|
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# NumberToText
|
2
|
+
A Ruby gem for converting numbers to their text equivalent (e.g. 1000 = One Thousand). The limit of the converter 999999999999 but can be easily extended to convert larger numbers.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'numbertotext-ruby'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install numbertotext-ruby
|
16
|
+
|
17
|
+
|
18
|
+
## Contributing
|
19
|
+
|
20
|
+
1. Fork it
|
21
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
22
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
23
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
24
|
+
5. Create new Pull Request
|
data/lib/numbertotext.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'numbertotext/version' unless defined?(NumberToText::VERSION)
|
2
|
+
|
3
|
+
module NumberToText
|
4
|
+
|
5
|
+
@dictionary1 =['zero','one','two','three','four','five','six','seven','eight','nine','ten',
|
6
|
+
'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
|
7
|
+
|
8
|
+
@dictionary2 = ['zero','ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']
|
9
|
+
|
10
|
+
#generic convert: uses recursion to convert numbers above 100
|
11
|
+
private
|
12
|
+
def self.generic_convert(number,multiple,text)
|
13
|
+
whole = number / multiple
|
14
|
+
remainder = number - whole * multiple
|
15
|
+
return convert(whole) + ' ' + text if remainder == 0
|
16
|
+
return convert(whole) + ' ' + text + ' and ' + convert(remainder) if (remainder < 100 && remainder > 0)
|
17
|
+
return convert(whole) + ' ' + text + ', ' + convert(remainder) if remainder > 99
|
18
|
+
end
|
19
|
+
|
20
|
+
#special case for 20 to 99: requires lookup on dictionary two and numbers are formatted with a hyphen in the middle. also uses recursion to convert remainder
|
21
|
+
private
|
22
|
+
def self.convert_20_to_99(number)
|
23
|
+
tens = number / 10
|
24
|
+
remainder = number - tens * 10
|
25
|
+
return @dictionary2[tens] if remainder == 0
|
26
|
+
return @dictionary2[tens] + '-' + convert(remainder) if remainder > 0
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def self.convert(number)
|
31
|
+
if number < 20
|
32
|
+
return @dictionary1[number]
|
33
|
+
elsif number < 100 && number > 19
|
34
|
+
convert_20_to_99(number)
|
35
|
+
elsif number < 1000 && number > 99
|
36
|
+
return generic_convert(number,100,'hundred')
|
37
|
+
elsif number < 1000000 && number > 999
|
38
|
+
return generic_convert(number,1000,'thousand')
|
39
|
+
elsif number < 1000000000 && number > 999999
|
40
|
+
return generic_convert(number,1000000,'million')
|
41
|
+
elsif number < 1000000000000 && number > 999999999
|
42
|
+
return generic_convert(number,1000000000,'billion')
|
43
|
+
else
|
44
|
+
return 'number too large. please try with a number lower than or equal to 999999999999'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'numbertotext/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'numbertotext-ruby'
|
7
|
+
spec.version = NumberToText::VERSION
|
8
|
+
spec.authors = ['Edem Kumodzi']
|
9
|
+
spec.email = ['edem@kumodzi.com']
|
10
|
+
spec.summary = 'A simple library for converting numbers to their text equivalent (e.g. 1000 = One Thousand)'
|
11
|
+
spec.description = 'A simple library for converting numbers to their text equivalent (e.g. 1000 = One Thousand)'
|
12
|
+
spec.homepage = 'https://github.com/edemkumodzi/numbertotext-ruby'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
spec.required_ruby_version = '>= 1.9.3'
|
19
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NumberToText do
|
4
|
+
|
5
|
+
it 'should convert 0 to 20 to equivalent text' do
|
6
|
+
ntt = NumberToText
|
7
|
+
actual = [ntt.convert(0),ntt.convert(1),ntt.convert(2),ntt.convert(3),ntt.convert(4),ntt.convert(5),
|
8
|
+
ntt.convert(6),ntt.convert(7),ntt.convert(8),ntt.convert(9),ntt.convert(10),
|
9
|
+
ntt.convert(11),ntt.convert(12),ntt.convert(13),ntt.convert(14),ntt.convert(15),
|
10
|
+
ntt.convert(16),ntt.convert(17),ntt.convert(18),ntt.convert(19)]
|
11
|
+
|
12
|
+
expected = ['zero','one','two','three','four','five',
|
13
|
+
'six','seven','eight','nine','ten',
|
14
|
+
'eleven','twelve','thirteen','fourteen','fifteen',
|
15
|
+
'sixteen','seventeen','eighteen','nineteen']
|
16
|
+
expect(actual).to eq(expected)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should convert 20' do
|
20
|
+
actual = NumberToText.convert(20)
|
21
|
+
expected = 'twenty'
|
22
|
+
expect(actual).to eq(expected)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should convert 21' do
|
26
|
+
actual = NumberToText.convert(21)
|
27
|
+
expected = 'twenty-one'
|
28
|
+
expect(actual).to eq(expected)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should convert 30' do
|
32
|
+
actual = NumberToText.convert(30)
|
33
|
+
expected = 'thirty'
|
34
|
+
expect(actual).to eq(expected)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should convert 37' do
|
38
|
+
actual = NumberToText.convert(37)
|
39
|
+
expected = 'thirty-seven'
|
40
|
+
expect(actual).to eq(expected)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should convert 40' do
|
44
|
+
actual = NumberToText.convert(40)
|
45
|
+
expected = 'fourty'
|
46
|
+
expect(actual).to eq(expected)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should convert 49' do
|
50
|
+
actual = NumberToText.convert(49)
|
51
|
+
expected = 'fourty-nine'
|
52
|
+
expect(actual).to eq(expected)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should convert 100' do
|
56
|
+
actual = NumberToText.convert(100)
|
57
|
+
expected = 'one hundred'
|
58
|
+
expect(actual).to eq(expected)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should convert 101' do
|
62
|
+
actual = NumberToText.convert(101)
|
63
|
+
expected = 'one hundred and one'
|
64
|
+
expect(actual).to eq(expected)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should convert 121' do
|
68
|
+
actual = NumberToText.convert(121)
|
69
|
+
expected = 'one hundred and twenty-one'
|
70
|
+
expect(actual).to eq(expected)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should convert 999' do
|
74
|
+
actual = NumberToText.convert(999)
|
75
|
+
expected = 'nine hundred and ninety-nine'
|
76
|
+
expect(actual).to eq(expected)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should convert 1000' do
|
80
|
+
actual = NumberToText.convert(1000)
|
81
|
+
expected = 'one thousand'
|
82
|
+
expect(actual).to eq(expected)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should convert 1021' do
|
86
|
+
actual = NumberToText.convert(1021)
|
87
|
+
expected = 'one thousand and twenty-one'
|
88
|
+
expect(actual).to eq(expected)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should convert 1135' do
|
92
|
+
actual = NumberToText.convert(1135)
|
93
|
+
expected = 'one thousand, one hundred and thirty-five'
|
94
|
+
expect(actual).to eq(expected)
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should convert 9999' do
|
98
|
+
actual = NumberToText.convert(9999)
|
99
|
+
expected = 'nine thousand, nine hundred and ninety-nine'
|
100
|
+
expect(actual).to eq(expected)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should convert 999999' do
|
104
|
+
actual = NumberToText.convert(999999)
|
105
|
+
expected = 'nine hundred and ninety-nine thousand, nine hundred and ninety-nine'
|
106
|
+
expect(actual).to eq(expected)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should convert 1000000' do
|
110
|
+
actual = NumberToText.convert(1000000)
|
111
|
+
expected = 'one million'
|
112
|
+
expect(actual).to eq(expected)
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
it 'should convert 1000001' do
|
117
|
+
actual = NumberToText.convert(1000001)
|
118
|
+
expected = 'one million and one'
|
119
|
+
expect(actual).to eq(expected)
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should convert 999999999' do
|
123
|
+
actual = NumberToText.convert(999999999)
|
124
|
+
expected = 'nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine'
|
125
|
+
expect(actual).to eq(expected)
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'should convert 1000000000' do
|
129
|
+
actual = NumberToText.convert(1000000000)
|
130
|
+
expected = 'one billion'
|
131
|
+
expect(actual).to eq(expected)
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should convert 1000000001' do
|
135
|
+
actual = NumberToText.convert(1000000001)
|
136
|
+
expected = 'one billion and one'
|
137
|
+
expect(actual).to eq(expected)
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should convert 1000000001' do
|
141
|
+
actual = NumberToText.convert(1000000001)
|
142
|
+
expected = 'one billion and one'
|
143
|
+
expect(actual).to eq(expected)
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'should not convert above 999999999999' do
|
147
|
+
actual = NumberToText.convert(9999999999999)
|
148
|
+
expected = 'number too large. please try with a number lower than or equal to 999999999999'
|
149
|
+
expect(actual).to eq(expected)
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: numbertotext-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Edem Kumodzi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple library for converting numbers to their text equivalent (e.g.
|
14
|
+
1000 = One Thousand)
|
15
|
+
email:
|
16
|
+
- edem@kumodzi.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- Gemfile
|
22
|
+
- Gemfile.lock
|
23
|
+
- Guardfile
|
24
|
+
- README.md
|
25
|
+
- lib/numbertotext.rb
|
26
|
+
- lib/numbertotext/version.rb
|
27
|
+
- numbertotext.gemspec
|
28
|
+
- spec/lib/numbertotext_spec.rb
|
29
|
+
- spec/spec_helper.rb
|
30
|
+
homepage: https://github.com/edemkumodzi/numbertotext-ruby
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
metadata: {}
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 1.9.3
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 2.4.1
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: A simple library for converting numbers to their text equivalent (e.g. 1000
|
54
|
+
= One Thousand)
|
55
|
+
test_files:
|
56
|
+
- spec/lib/numbertotext_spec.rb
|
57
|
+
- spec/spec_helper.rb
|