integer_to_english 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c689cf8ada32860cd7b4ec850835856d0272fe10
4
+ data.tar.gz: e4a79c1507318e2247b302d25ee34295bdcb6dbd
5
+ SHA512:
6
+ metadata.gz: a54891c18fdd5c016e8f7f4bb6e19387b9e0f5c80172862bce550c41a20af2f13df117d57ebb051bd2527003d133d0d95f20f3a573c1754f41eaf05b4a71012a
7
+ data.tar.gz: 6b0ba8e1ae0323fd5fd2e4255a5c35d3070062a57157b25b90aa95d11e28b7bb8abe66849b0d41a1bd1c241c64a4d15e9bbfc720ed21e5f7692c0c4bf4cb327d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in integer_to_english.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'minitest-reporters'
8
+ gem 'pry'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Viktor Vad
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # IntegerToEnglish
2
+
3
+ The ```Integer``` class is pimped up with the ```to_english``` method.
4
+
5
+ Regarding the names of numbers above million, the "short scale" will be used as it is used increasingly in British English too.
6
+ English grammar source used: http://en.wikipedia.org/wiki/English-language_numerals
7
+
8
+ **Limitation**: The highest number it can convert is less then 1 quintillion (10^18)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'integer_to_english'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install integer_to_english
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ 7.to_english
30
+ 'seven'
31
+ ```
32
+
33
+ ```ruby
34
+ 42to_english
35
+ 'forty-two'
36
+ ```
37
+
38
+ ```ruby
39
+ 2001.to_english
40
+ 'two thousand and one'
41
+ ```
42
+
43
+ ```ruby
44
+ 1999.to_english
45
+ 'nineteen hundred and ninety-nine'
46
+ ```
47
+
48
+ US vs UK (UK is default, if you think otherwise, "fork it!")
49
+
50
+ ```ruby
51
+ 999_000.to_english
52
+ 'nine hundred and ninety-nine thousand'
53
+ ```
54
+
55
+ ```ruby
56
+ 999_000.to_english :us
57
+ 'nine hundred ninety-nine thousand'
58
+ ```
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( https://github.com/vadviktor/integer_to_english/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ task :default => :test
9
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ require 'pathname'
3
+ require Pathname.new(__dir__) + 'lib/integer_to_english/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'integer_to_english'
7
+ spec.version = IntegerToEnglish::VERSION
8
+ spec.authors = ['Viktor Vad']
9
+ spec.email = ['vad.viktor@gmail.com']
10
+ spec.summary = %q{Integer.to_english}
11
+ spec.description = %q{The Integer class is pimped up with the to_english method.}
12
+ spec.homepage = 'https://github.com/vadviktor/integer_to_english'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.7'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'minitest'
23
+ end
@@ -0,0 +1,145 @@
1
+ require 'pathname'
2
+ require Pathname.new(__dir__) + 'integer_to_english/version'
3
+
4
+ module IntegerToEnglish
5
+
6
+ # @params [Symbol] format
7
+ def to_english(format = :gb)
8
+ Converter.new.to_text self, format
9
+ end
10
+
11
+ class Converter
12
+
13
+ def initialize
14
+ @singles = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
15
+ @tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
16
+ @ten_frags = ['', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
17
+ @scale = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion'] # can be extended further
18
+ end
19
+
20
+ # Version can be :gb or :uk for British or :us for USA
21
+ # "and" word will be used accordingly
22
+ #
23
+ # @param number [String|Integer]
24
+ # @param version [Symbol]
25
+ # @return [String]
26
+ def to_text(number, version = :gb)
27
+ unless number.is_a?(String) || number.is_a?(Integer)
28
+ raise ArgumentError.new 'Only Integer or String (that converts to integer) can be parsed'
29
+ end
30
+
31
+ version = version.to_sym if version.is_a? String
32
+ unless [:uk, :gb, :us].include? version
33
+ raise ArgumentError.new 'Only :uk, :gb or :us versions can be generated'
34
+ end
35
+
36
+ number = number.to_s if number.is_a? Integer
37
+ number.strip!
38
+
39
+ # strip prefix
40
+ number[0] = '' if number[0] == '+' or number[0] == '-'
41
+
42
+ # 0
43
+ return 'zero' if number.length == 1 and number == '0'
44
+
45
+ # 1..999
46
+ return get_hundreds number, version if number.length < 4
47
+
48
+ # 999 <
49
+ i = 0
50
+ portions = []
51
+ portion_block = []
52
+ number.reverse!
53
+
54
+ number.each_char do |c|
55
+ portion_block.push c
56
+ i += 1
57
+
58
+ if i % 3 == 0
59
+ block_clone = portion_block.clone
60
+ portions.push block_clone
61
+ portion_block.clear
62
+ i = 0
63
+ end
64
+ end
65
+
66
+ portions.push portion_block unless portion_block.empty?
67
+ portions.reverse!
68
+
69
+ final_number = ''
70
+ i = portions.length - 1
71
+ portions.each do |p|
72
+ p.reverse!
73
+ p_in_text = get_hundreds(p.join.to_i.to_s, version)
74
+ final_number += p_in_text
75
+ final_number += ' ' + @scale[i] + ' ' if p_in_text != ''
76
+ i -= 1
77
+ end
78
+ final_number.strip!
79
+ end
80
+
81
+ private
82
+
83
+ # @param number [String]
84
+ # @return [String]
85
+ def get_hundreds number, version
86
+ two = get_2_decimals number
87
+ return two unless two.nil?
88
+ get_3_decimals number, version
89
+ end
90
+
91
+ # @param number [String]
92
+ # @return [String|Nil]
93
+ def get_2_decimals number
94
+ # 0
95
+ return '' if number.length == 1 and number == '0'
96
+
97
+ # 1..9 / 01..09
98
+ if (number.length == 1 and number != '0') or
99
+ (number.length == 2 and number[0] == '0' and number[1] != '0')
100
+ return @singles[number.to_i]
101
+ end
102
+
103
+ # tens
104
+ if number.length == 2 and number[0] != '0' and number[1] == '0'
105
+ return @tens[number[0].to_i]
106
+ end
107
+
108
+ # 11..19
109
+ if number.length == 2 and number[0] == '1' and number[1].to_i > 0
110
+ return @ten_frags[number[1].to_i]
111
+ end
112
+
113
+ # 21..99
114
+ if number.length == 2 and number[0].to_i > 1 and number[1].to_i >= 1
115
+ return @tens[number[0].to_i] + '-' + @singles[number[1].to_i]
116
+ end
117
+
118
+ nil
119
+ end
120
+
121
+ # @param number [String|Array]
122
+ # @return [String|Nil]
123
+ def get_3_decimals number, version
124
+ # hundreds
125
+ if number.length == 3 and number[0] != '0'
126
+ final_number = @singles[number[0].to_i] + ' hundred'
127
+
128
+ if number[1] != '0' or number[2] != '0'
129
+ final_number += version == :gb ? ' and ' : ' '
130
+ final_number += get_2_decimals(number[1..2])
131
+ end
132
+
133
+ return final_number
134
+ end
135
+
136
+ nil
137
+ end
138
+
139
+ end
140
+
141
+ end
142
+
143
+ class Integer
144
+ include IntegerToEnglish
145
+ end
@@ -0,0 +1,3 @@
1
+ module IntegerToEnglish
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'pathname'
2
+ require Pathname.new(__dir__).parent + 'lib/integer_to_english'
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/reporters'
6
+
7
+ if ENV['MINITEST_REPORTER'] == 'RUBYMINE'
8
+ Minitest::Reporters.use! Minitest::Reporters::RubyMineReporter.new
9
+ else
10
+ Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new
11
+ end
@@ -0,0 +1,125 @@
1
+ require 'pathname'
2
+ require Pathname.new(__dir__) + 'minitest_helper'
3
+
4
+ class TestIntegerToEnglish < MiniTest::Test
5
+ def test_that_it_has_a_version_number
6
+ refute_nil ::IntegerToEnglish::VERSION
7
+ end
8
+
9
+ def test_integer_augmented
10
+ assert 0.respond_to?(:to_english), 'Integer has not been taught to speak English'
11
+ end
12
+
13
+ def test_should_convert_0_to_9
14
+ assert_equal 'zero', 0.to_english
15
+ assert_equal 'one', 1.to_english
16
+ assert_equal 'two', 2.to_english
17
+ assert_equal 'three', 3.to_english
18
+ assert_equal 'four', 4.to_english
19
+ assert_equal 'five', 5.to_english
20
+ assert_equal 'six', 6.to_english
21
+ assert_equal 'seven', 7.to_english
22
+ assert_equal 'eight', 8.to_english
23
+ assert_equal 'nine', 9.to_english
24
+ end
25
+
26
+ def test_should_convert_10s
27
+ assert_equal 'ten', 10.to_english
28
+ assert_equal 'twenty', 20.to_english
29
+ assert_equal 'thirty', 30.to_english
30
+ assert_equal 'forty', 40.to_english
31
+ assert_equal 'fifty', 50.to_english
32
+ assert_equal 'sixty', 60.to_english
33
+ assert_equal 'seventy', 70.to_english
34
+ assert_equal 'eighty', 80.to_english
35
+ assert_equal 'ninety', 90.to_english
36
+ end
37
+
38
+ def test_should_convert_11_to_19
39
+ assert_equal 'eleven', 11.to_english
40
+ assert_equal 'twelve', 12.to_english
41
+ assert_equal 'thirteen', 13.to_english
42
+ assert_equal 'fourteen', 14.to_english
43
+ assert_equal 'fifteen', 15.to_english
44
+ assert_equal 'sixteen', 16.to_english
45
+ assert_equal 'seventeen', 17.to_english
46
+ assert_equal 'eighteen', 18.to_english
47
+ assert_equal 'nineteen', 19.to_english
48
+ end
49
+
50
+ def test_should_convert_21_to_99
51
+ assert_equal 'twenty-one', 21.to_english
52
+ assert_equal 'forty-two', 42.to_english
53
+ assert_equal 'sixty-five', 65.to_english
54
+ assert_equal 'ninety-nine', 99.to_english
55
+ end
56
+
57
+ def test_should_deal_with_prefix
58
+ assert_equal 'sixty-five', +65.to_english
59
+ assert_equal 'sixty-five', -65.to_english
60
+ end
61
+
62
+ def test_should_convert_100s
63
+ assert_equal 'one hundred', 100.to_english
64
+ assert_equal 'two hundred', 200.to_english
65
+ assert_equal 'nine hundred', 900.to_english
66
+ end
67
+
68
+ def test_should_convert_hundreds_in_usa
69
+ assert_equal 'two hundred seventy-three', 273.to_english(:us)
70
+ assert_equal 'five hundred six', 506.to_english(:us)
71
+ assert_equal 'four hundred fifty-three', 453.to_english('us')
72
+ end
73
+
74
+ def test_should_convert_hundreds_in_british
75
+ assert_equal 'two hundred and seventy-three', 273.to_english
76
+ assert_equal 'five hundred and six', 506.to_english
77
+ end
78
+
79
+ def test_should_convert_1000s
80
+ assert_equal 'one thousand', 1000.to_english
81
+ assert_equal 'two thousand', 2000.to_english
82
+ assert_equal 'nine thousand', 9000.to_english
83
+ end
84
+
85
+ def test_should_convert_millions
86
+ assert_equal 'one million', 1000000.to_english
87
+ assert_equal 'two million', 2000000.to_english
88
+ assert_equal 'nine million', 9000000.to_english
89
+ end
90
+
91
+ def test_should_convert_billions
92
+ assert_equal 'one billion', 1000000000.to_english
93
+ assert_equal 'two billion', 2000000000.to_english
94
+ assert_equal 'nine billion', 9000000000.to_english
95
+ end
96
+
97
+ def test_should_convert_trillions
98
+ assert_equal 'one trillion', 1000000000000.to_english
99
+ assert_equal 'two trillion', 2000000000000.to_english
100
+ assert_equal 'nine trillion', 9000000000000.to_english
101
+ end
102
+
103
+ def test_should_convert_quadrillions
104
+ assert_equal 'one quadrillion', 1000000000000000.to_english
105
+ assert_equal 'two quadrillion', 2000000000000000.to_english
106
+ assert_equal 'nine quadrillion', 9000000000000000.to_english
107
+ end
108
+
109
+ # TODO more tricky and complex numbers
110
+
111
+ #list of random numbers, should be listing tricky ones
112
+ def test_should_convert_any_numbers_it_could
113
+ assert_equal 'forty-three thousand four hundred and two', 43402.to_english
114
+ assert_equal 'three hundred thirty thousand four hundred two', 330402.to_english(:us)
115
+ assert_equal(
116
+ 'six hundred twenty-three quadrillion seven hundred seventy-seven trillion four million three thousand four hundred two',
117
+ 623777000004003402.to_english(:us)
118
+ )
119
+ assert_equal(
120
+ 'six hundred and twenty-three quadrillion seven hundred and seventy-seven trillion four million three thousand four hundred and two',
121
+ 623777000004003402.to_english(:gb)
122
+ )
123
+
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: integer_to_english
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Viktor Vad
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: The Integer class is pimped up with the to_english method.
56
+ email:
57
+ - vad.viktor@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - integer_to_english.gemspec
68
+ - lib/integer_to_english.rb
69
+ - lib/integer_to_english/version.rb
70
+ - test/minitest_helper.rb
71
+ - test/test_integer_to_english.rb
72
+ homepage: https://github.com/vadviktor/integer_to_english
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.5
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Integer.to_english
96
+ test_files:
97
+ - test/minitest_helper.rb
98
+ - test/test_integer_to_english.rb