numbers_in_words 0.3.0 → 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 +5 -5
- data/.codeclimate.yml +2 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.rubocop.yml +35 -1148
- data/.travis.yml +14 -4
- data/Gemfile +4 -1
- data/README.md +6 -43
- data/Rakefile +3 -1
- data/bin/spec +0 -0
- data/lib/numbers_in_words.rb +44 -19
- data/lib/numbers_in_words/duck_punch.rb +12 -8
- data/lib/numbers_in_words/exceptional_numbers.rb +115 -0
- data/lib/numbers_in_words/fraction.rb +136 -0
- data/lib/numbers_in_words/number_group.rb +34 -25
- data/lib/numbers_in_words/parsing/fraction_parsing.rb +34 -0
- data/lib/numbers_in_words/parsing/number_parser.rb +98 -0
- data/lib/numbers_in_words/parsing/pair_parsing.rb +64 -0
- data/lib/numbers_in_words/parsing/parse_fractions.rb +45 -0
- data/lib/numbers_in_words/parsing/parse_individual_number.rb +68 -0
- data/lib/numbers_in_words/parsing/parse_status.rb +17 -0
- data/lib/numbers_in_words/parsing/special.rb +67 -0
- data/lib/numbers_in_words/parsing/to_number.rb +77 -0
- data/lib/numbers_in_words/powers_of_ten.rb +49 -0
- data/lib/numbers_in_words/to_word.rb +78 -13
- data/lib/numbers_in_words/version.rb +3 -1
- data/lib/numbers_in_words/writer.rb +69 -0
- data/numbers_in_words.gemspec +14 -13
- data/spec/exceptional_numbers_spec.rb +26 -0
- data/spec/fraction_spec.rb +152 -0
- data/spec/fractions_spec.rb +31 -0
- data/spec/non_monkey_patch_spec.rb +40 -15
- data/spec/number_group_spec.rb +12 -12
- data/spec/number_parser_spec.rb +31 -0
- data/spec/numbers_in_words_spec.rb +63 -70
- data/spec/numerical_strings_spec.rb +35 -0
- data/spec/spec_helper.rb +24 -4
- data/spec/to_word_spec.rb +18 -0
- data/spec/words_in_numbers_spec.rb +133 -116
- data/spec/writer_spec.rb +26 -0
- data/spec/years_spec.rb +27 -0
- metadata +49 -27
- data/lib/numbers_in_words/english/constants.rb +0 -93
- data/lib/numbers_in_words/english/language_writer_english.rb +0 -109
- data/lib/numbers_in_words/language_writer.rb +0 -31
- data/lib/numbers_in_words/number_parser.rb +0 -81
- data/lib/numbers_in_words/to_number.rb +0 -82
- data/spec/language_writer_spec.rb +0 -23
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe NumbersInWords do
|
4
|
+
FRACTION_EXAMPLES = {
|
5
|
+
'half' => 0.5,
|
6
|
+
'a half' => 0.5,
|
7
|
+
'one half' => 0.5,
|
8
|
+
'two halves' => 1.0,
|
9
|
+
'three halves' => 1.5,
|
10
|
+
'three quarters' => 0.75,
|
11
|
+
'three fifths' => 3 / 5.0,
|
12
|
+
'seven fifteenths' => 7 / 15.0
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
PENDING = {
|
16
|
+
'twenty and three fifteenths' => 20 + 3 / 15.0,
|
17
|
+
'three ninety-sevenths' => 3 / 97.0
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
FRACTION_EXAMPLES.each do |string, float|
|
21
|
+
it "#{string} == #{float}" do
|
22
|
+
expect(string.in_numbers).to eql(float)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
PENDING.each do |string, float|
|
27
|
+
pending "#{string} == #{float}" do
|
28
|
+
expect(string.in_numbers).to eql(float)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,26 +1,51 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'NumbersInWords' do # rubocop: disable Metrics/BlockLength
|
6
|
+
describe '.in_words' do
|
5
7
|
it do
|
6
|
-
expect(NumbersInWords.in_words(100)).to eq
|
7
|
-
expect(NumbersInWords.in_words(-100)).to eq
|
8
|
-
expect(NumbersInWords.in_words(24)).to eq
|
9
|
-
expect(NumbersInWords.in_words(1.2)).to eq
|
10
|
-
expect(NumbersInWords.in_words(100*10**100)).to eq
|
11
|
-
expect(NumbersInWords.in_words(30 + 100*10**100)).to eq
|
8
|
+
expect(NumbersInWords.in_words(100)).to eq 'one hundred'
|
9
|
+
expect(NumbersInWords.in_words(-100)).to eq 'minus one hundred'
|
10
|
+
expect(NumbersInWords.in_words(24)).to eq 'twenty-four'
|
11
|
+
expect(NumbersInWords.in_words(1.2)).to eq 'one point two'
|
12
|
+
expect(NumbersInWords.in_words(100 * 10**100)).to eq 'one hundred googol'
|
13
|
+
expect(NumbersInWords.in_words(30 + 100 * 10**100)).to eq 'one hundred googol and thirty'
|
12
14
|
end
|
15
|
+
end
|
16
|
+
|
17
|
+
FRACTION_TO_WORDS = {
|
18
|
+
[1, 2] => 'one half',
|
19
|
+
[3, 2] => 'three halves',
|
20
|
+
[1, 3] => 'one third',
|
21
|
+
[1, 4] => 'one quarter',
|
22
|
+
[1, 5] => 'one fifth',
|
23
|
+
[1, 19] => 'one nineteenth',
|
24
|
+
[2, 17] => 'two seventeenths',
|
25
|
+
[1, 21] => 'one twenty-first',
|
26
|
+
[1, 32] => 'one thirty-second',
|
27
|
+
[1, 101] => 'one one hundred and first',
|
28
|
+
[3, 101] => 'three hundred and firsts',
|
29
|
+
[73, 102] => 'seventy-three hundred and seconds',
|
30
|
+
[13, 97] => 'thirteen ninety-sevenths'
|
31
|
+
}.freeze
|
13
32
|
|
14
|
-
|
15
|
-
|
33
|
+
FRACTION_TO_WORDS.each do |(numerator, denominator), string|
|
34
|
+
it "#{numerator}/#{denominator} == #{string}" do
|
35
|
+
expect(NumbersInWords.in_words(numerator.to_f / denominator, fraction: true)).to eql(string)
|
16
36
|
end
|
17
37
|
end
|
18
38
|
|
19
|
-
describe
|
39
|
+
describe '.in_numbers' do
|
20
40
|
it do
|
21
|
-
expect(NumbersInWords.in_numbers(
|
41
|
+
expect(NumbersInWords.in_numbers('hundred')).to eq 100
|
42
|
+
expect(NumbersInWords.in_numbers('one hundred')).to eq 100
|
43
|
+
|
44
|
+
expect(NumbersInWords.in_numbers('minus one hundred')).to eq(-100)
|
45
|
+
expect(NumbersInWords.in_numbers('twenty four')).to eq 24
|
46
|
+
expect(NumbersInWords.in_numbers('one point two')).to eq 1.2
|
47
|
+
expect(NumbersInWords.in_numbers('one hundred googol')).to eq 100 * 10**100
|
48
|
+
expect(NumbersInWords.in_numbers('one hundred googol and thirty')).to eq 30 + 100 * 10**100
|
22
49
|
end
|
23
50
|
end
|
24
|
-
|
25
|
-
|
26
51
|
end
|
data/spec/number_group_spec.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require './spec/spec_helper'
|
4
|
+
require 'numbers_in_words/number_group'
|
2
5
|
|
3
6
|
describe NumbersInWords::NumberGroup do
|
4
|
-
it
|
5
|
-
g =
|
6
|
-
|
7
|
-
expect(g.groups_of(
|
8
|
-
expect(g.groups_of(
|
9
|
-
expect(g.groups_of(
|
10
|
-
expect(g.groups_of(
|
11
|
-
expect(g.groups_of(
|
12
|
-
expect(g.groups_of(
|
13
|
-
expect(g.groups_of(123_456_789_101_112 ,3 )).to eq({12=>123 , 9=>456 , 6=>789 , 3=>101 , 0=>112 })
|
7
|
+
it 'should split into group of three digit numbers' do
|
8
|
+
g = described_class
|
9
|
+
expect(g.groups_of(1, 3)).to eq({ 0 => 1 })
|
10
|
+
expect(g.groups_of(12, 3)).to eq({ 0 => 12 })
|
11
|
+
expect(g.groups_of(123, 3)).to eq({ 0 => 123 })
|
12
|
+
expect(g.groups_of(1111, 3)).to eq({ 3 => 1, 0 => 111 })
|
13
|
+
expect(g.groups_of(87_654, 3)).to eq({ 3 => 87, 0 => 654 })
|
14
|
+
expect(g.groups_of(1_234_567, 3)).to eq({ 6 => 1, 3 => 234, 0 => 567 })
|
15
|
+
expect(g.groups_of(123_456_789_101_112, 3)).to eq({ 12 => 123, 9 => 456, 6 => 789, 3 => 101, 0 => 112 })
|
14
16
|
end
|
15
17
|
end
|
16
|
-
|
17
|
-
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './spec/spec_helper'
|
4
|
+
require 'numbers_in_words/parsing/number_parser'
|
5
|
+
|
6
|
+
describe NumbersInWords::NumberParser do
|
7
|
+
subject do
|
8
|
+
super().parse(number)
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'with single digit' do
|
12
|
+
let(:number) { [9] }
|
13
|
+
it { expect(subject).to eq 9 }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'with thousands' do
|
17
|
+
let(:number) { [20, 1000] }
|
18
|
+
it { expect(subject).to eq 20_000 }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with thousands with a leading one' do
|
22
|
+
let(:number) { [1, 1000, 6, 100] }
|
23
|
+
it { expect(subject).to eq 1600 }
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with hundreds' do
|
27
|
+
let(:number) { [2, 100, 45] }
|
28
|
+
|
29
|
+
it { expect(subject).to eq 245 }
|
30
|
+
end
|
31
|
+
end
|
@@ -1,106 +1,99 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
describe NumbersInWords do
|
4
|
-
describe ".language" do
|
5
|
-
after do
|
6
|
-
NumbersInWords.instance_variable_set(:"@language", nil)
|
7
|
-
end
|
8
|
-
|
9
|
-
it "has a default value" do
|
10
|
-
expect(NumbersInWords.language).to eq "English"
|
11
|
-
end
|
1
|
+
# frozen_string_literal: true
|
12
2
|
|
13
|
-
|
14
|
-
NumbersInWords.language = "some language"
|
15
|
-
expect(NumbersInWords.language).to eq "some language"
|
16
|
-
end
|
17
|
-
end
|
3
|
+
require './spec/spec_helper'
|
18
4
|
|
19
|
-
|
5
|
+
describe NumbersInWords do # rubocop: disable Metrics/BlockLength
|
6
|
+
describe '.in_words' do
|
20
7
|
it do
|
21
|
-
expect(NumbersInWords.in_words(100)).to eq
|
8
|
+
expect(NumbersInWords.in_words(100)).to eq 'one hundred'
|
22
9
|
end
|
23
10
|
end
|
24
11
|
|
25
|
-
describe
|
12
|
+
describe '.in_numbers' do
|
26
13
|
it do
|
27
|
-
expect(NumbersInWords.in_numbers(
|
14
|
+
expect(NumbersInWords.in_numbers('one-hundred')).to eq 100
|
28
15
|
end
|
29
16
|
end
|
30
17
|
|
31
|
-
it
|
18
|
+
it 'should print the digits 0-9 correctly' do
|
32
19
|
numbers = %w[zero one two three four five six seven eight nine]
|
33
20
|
|
34
21
|
10.times { |i| expect(i.in_words).to eq(numbers[i]) }
|
35
22
|
end
|
36
23
|
|
37
|
-
it
|
24
|
+
it 'should print the digits 11-19 correctly' do
|
38
25
|
words = %w[eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen]
|
39
|
-
numbers = [11,12,13,14,15,16,17,18,19]
|
26
|
+
numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19]
|
40
27
|
numbers.each_with_index do |number, index|
|
41
28
|
expect(number.in_words).to eq(words[index])
|
42
|
-
|
43
29
|
end
|
44
30
|
end
|
45
31
|
|
46
|
-
it
|
47
|
-
expect(21.in_words).
|
32
|
+
it 'should handle two digit numbers' do
|
33
|
+
expect(21.in_words).to eq('twenty-one')
|
48
34
|
end
|
49
35
|
|
50
|
-
it
|
51
|
-
expect(113.in_words).
|
52
|
-
expect(299.in_words).
|
53
|
-
expect(300.in_words).
|
54
|
-
expect(101.in_words).
|
36
|
+
it 'should handle three digit numbers' do
|
37
|
+
expect(113.in_words).to eq('one hundred and thirteen')
|
38
|
+
expect(299.in_words).to eq('two hundred and ninety-nine')
|
39
|
+
expect(300.in_words).to eq('three hundred')
|
40
|
+
expect(101.in_words).to eq('one hundred and one')
|
55
41
|
end
|
56
42
|
|
57
|
-
it
|
58
|
-
expect(2999
|
59
|
-
expect(
|
60
|
-
expect(
|
61
|
-
expect(
|
62
|
-
expect(
|
63
|
-
expect(
|
64
|
-
expect(
|
65
|
-
expect(
|
66
|
-
expect(
|
67
|
-
expect(
|
68
|
-
expect(
|
69
|
-
expect(
|
43
|
+
it 'should print out some random examples correctly' do
|
44
|
+
expect(2999.in_words).to eq('two thousand nine hundred and ninety-nine')
|
45
|
+
expect(99_999.in_words).to eq('ninety-nine thousand nine hundred and ninety-nine')
|
46
|
+
expect(999_999.in_words).to eq('nine hundred and ninety-nine thousand nine hundred and ninety-nine')
|
47
|
+
expect(123_456.in_words).to eq('one hundred and twenty-three thousand four hundred and fifty-six')
|
48
|
+
expect(24_056.in_words).to eq('twenty-four thousand and fifty-six')
|
49
|
+
expect(17_054.in_words).to eq('seventeen thousand and fifty-four')
|
50
|
+
expect(11_004.in_words).to eq('eleven thousand and four')
|
51
|
+
expect(470_154.in_words).to eq('four hundred and seventy thousand one hundred and fifty-four')
|
52
|
+
expect(417_155.in_words).to eq('four hundred and seventeen thousand one hundred and fifty-five')
|
53
|
+
expect(999_999.in_words).to eq('nine hundred and ninety-nine thousand nine hundred and ninety-nine')
|
54
|
+
expect(1_000_000.in_words).to eq('one million')
|
55
|
+
expect(1_000_001.in_words).to eq('one million and one')
|
56
|
+
expect(112.in_words).to eq('one hundred and twelve')
|
70
57
|
end
|
71
58
|
|
72
|
-
it
|
73
|
-
expect(
|
74
|
-
expect((10*10**12 + 10**6 +1)
|
75
|
-
expect((10**75)
|
76
|
-
expect(
|
59
|
+
it 'should handle edge cases' do
|
60
|
+
expect(1_000_001.in_words).to eq('one million and one')
|
61
|
+
expect((10 * 10**12 + 10**6 + 1).in_words).to eq('ten trillion one million and one')
|
62
|
+
expect((10**75).in_words).to eq('one quattuorvigintillion')
|
63
|
+
expect(10_001_001.in_words).to eq('ten million one thousand and one')
|
77
64
|
end
|
78
65
|
|
79
|
-
it
|
80
|
-
|
81
|
-
expect((
|
82
|
-
expect((42*
|
83
|
-
|
66
|
+
it 'should handle a googol and larger' do
|
67
|
+
googol = 10**100
|
68
|
+
expect((googol + 1).in_words).to eq('one googol and one')
|
69
|
+
expect((42 * googol + 16_777_216).in_words)
|
70
|
+
.to eq('forty-two googol sixteen million seven hundred and seventy-seven thousand two hundred and sixteen')
|
71
|
+
expect((42 * googol * googol).in_words).to eq('forty-two googol googol')
|
84
72
|
end
|
85
73
|
|
86
|
-
it
|
87
|
-
expect(-1
|
88
|
-
expect(-9
|
89
|
-
expect(-10
|
90
|
-
expect(-15
|
91
|
-
expect(-100
|
92
|
-
expect((-1*(10**100)).in_words).to eq(
|
93
|
-
|
74
|
+
it 'should handle negative numbers' do
|
75
|
+
expect(-1.in_words).to eq('minus one')
|
76
|
+
expect(-9.in_words).to eq('minus nine')
|
77
|
+
expect(-10.in_words).to eq('minus ten')
|
78
|
+
expect(-15.in_words).to eq('minus fifteen')
|
79
|
+
expect(-100.in_words).to eq('minus one hundred')
|
80
|
+
expect((-1 * (10**100)).in_words).to eq('minus one googol')
|
81
|
+
|
82
|
+
expect(-123_456_789.in_words)
|
83
|
+
.to eq('minus one hundred and twenty-three million four hundred and ' \
|
84
|
+
'fifty-six thousand seven hundred and eighty-nine')
|
94
85
|
end
|
95
86
|
|
96
|
-
it
|
97
|
-
#because of lack of absolute accuracy with floats
|
98
|
-
#the output won't be exactly as you might expect
|
99
|
-
#so we will match rather than find equivalents
|
100
|
-
expect(1.1
|
101
|
-
expect(1.2345678
|
102
|
-
expect(1000.2345678
|
103
|
-
expect(
|
87
|
+
it 'should handle decimals' do
|
88
|
+
# because of lack of absolute accuracy with floats
|
89
|
+
# the output won't be exactly as you might expect
|
90
|
+
# so we will match rather than find equivalents
|
91
|
+
expect(1.1.in_words).to match(/one point one/)
|
92
|
+
expect(1.2345678.in_words).to match(/one point two three four five six seven eight/)
|
93
|
+
expect(1000.2345678.in_words).to match(/one thousand point two three four five six seven eight/)
|
94
|
+
expect(12_345.2345678.in_words)
|
95
|
+
.to match(/twelve thousand three hundred and forty-five point two three four five six seven eight/)
|
96
|
+
|
104
97
|
expect((10**9 + 0.1).in_words).to match(/one billion point one/)
|
105
98
|
end
|
106
99
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './spec/spec_helper'
|
4
|
+
|
5
|
+
describe NumbersInWords do
|
6
|
+
it 'recognizes numerical strings' do
|
7
|
+
arr = %w[8 56 100 5789 3435356]
|
8
|
+
arr.each { |s| expect(s.in_numbers).to eql(s.to_f) }
|
9
|
+
end
|
10
|
+
|
11
|
+
MIXED = {
|
12
|
+
'19 hundred' => 1_900,
|
13
|
+
'20 thousand' => 20_000,
|
14
|
+
'100 million' => 100_000_000,
|
15
|
+
'7 billion' => 7_000_000_000,
|
16
|
+
'42 trillion' => 42_000_000_000_000,
|
17
|
+
'20 thousand and 4' => 20_004
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
MIXED.each do |k, v|
|
21
|
+
it do
|
22
|
+
expect(k.in_numbers).to eql(v)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
PENDING_MIXED = {
|
27
|
+
'19 zero five' => 1_905
|
28
|
+
}.freeze
|
29
|
+
|
30
|
+
PENDING_MIXED.each do |k, v|
|
31
|
+
pending do
|
32
|
+
expect(k.in_numbers).to eql(v)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
if ENV['CC_TEST_REPORTER_ID']
|
4
|
+
require 'simplecov'
|
5
|
+
SimpleCov.start
|
6
|
+
end
|
3
7
|
|
4
|
-
|
5
|
-
|
8
|
+
unless Kernel.method_defined?(:silence_warnings)
|
9
|
+
module Kernel
|
10
|
+
def silence_warnings
|
11
|
+
with_warnings(nil) { yield }
|
12
|
+
end
|
6
13
|
|
14
|
+
def with_warnings(flag)
|
15
|
+
old_verbose = $VERBOSE
|
16
|
+
$VERBOSE = flag
|
17
|
+
yield
|
18
|
+
ensure
|
19
|
+
$VERBOSE = old_verbose
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'numbers_in_words'
|
25
|
+
require 'numbers_in_words/duck_punch'
|
26
|
+
require 'byebug'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe NumbersInWords::ToWord do
|
4
|
+
let(:number) { 2_111 }
|
5
|
+
let(:writer) { described_class.new(number) }
|
6
|
+
|
7
|
+
describe '#in_words' do
|
8
|
+
let(:number) { 99 }
|
9
|
+
|
10
|
+
subject do
|
11
|
+
writer.in_words
|
12
|
+
end
|
13
|
+
|
14
|
+
it do
|
15
|
+
expect(subject).to eq 'ninety-nine'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,135 +1,152 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require './spec/spec_helper'
|
2
4
|
|
3
|
-
describe
|
4
|
-
it
|
5
|
-
expect(
|
6
|
-
expect(
|
7
|
-
expect(
|
8
|
-
expect(
|
9
|
-
expect(
|
10
|
-
expect(
|
11
|
-
expect(
|
12
|
-
expect(
|
13
|
-
expect(
|
14
|
-
expect(
|
5
|
+
describe NumbersInWords::StringExtension do # rubocop: disable Metrics/BlockLength
|
6
|
+
it 'should do the digits 0-10' do
|
7
|
+
expect('zero'.in_numbers).to eq(0)
|
8
|
+
expect('one'.in_numbers).to eq(1)
|
9
|
+
expect('two'.in_numbers).to eq(2)
|
10
|
+
expect('three'.in_numbers).to eq(3)
|
11
|
+
expect('four'.in_numbers).to eq(4)
|
12
|
+
expect('five'.in_numbers).to eq(5)
|
13
|
+
expect('six'.in_numbers).to eq(6)
|
14
|
+
expect('seven'.in_numbers).to eq(7)
|
15
|
+
expect('eight'.in_numbers).to eq(8)
|
16
|
+
expect('nine'.in_numbers).to eq(9)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should handle numbers for which there is one word' do
|
20
|
+
expect('ten'.in_numbers).to eq(10)
|
21
|
+
expect('eleven'.in_numbers).to eq(11)
|
22
|
+
expect('twelve'.in_numbers).to eq(12)
|
23
|
+
expect('thirteen'.in_numbers).to eq(13)
|
24
|
+
expect('fourteen'.in_numbers).to eq(14)
|
25
|
+
expect('fifteen'.in_numbers).to eq(15)
|
26
|
+
expect('sixteen'.in_numbers).to eq(16)
|
27
|
+
expect('seventeen'.in_numbers).to eq(17)
|
28
|
+
expect('eighteen'.in_numbers).to eq(18)
|
29
|
+
expect('nineteen'.in_numbers).to eq(19)
|
30
|
+
expect('twenty'.in_numbers).to eq(20)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should handle two word numbers up to 100' do
|
34
|
+
expect('twenty-one'.in_numbers).to eq(21)
|
35
|
+
expect('twenty-two'.in_numbers).to eq(22)
|
36
|
+
expect('twenty-three'.in_numbers).to eq(23)
|
37
|
+
expect('twenty-four'.in_numbers).to eq(24)
|
38
|
+
expect('twenty-five'.in_numbers).to eq(25)
|
39
|
+
expect('twenty-six'.in_numbers).to eq(26)
|
40
|
+
expect('twenty-seven'.in_numbers).to eq(27)
|
41
|
+
expect('twenty-eight'.in_numbers).to eq(28)
|
42
|
+
expect('seventy-six'.in_numbers).to eq(76)
|
43
|
+
expect('ninety-nine'.in_numbers).to eq(99)
|
15
44
|
end
|
16
45
|
|
17
|
-
it
|
18
|
-
expect(
|
19
|
-
expect(
|
20
|
-
expect(
|
21
|
-
expect(
|
22
|
-
expect(
|
23
|
-
expect(
|
24
|
-
expect(
|
25
|
-
expect("seventeen" .in_numbers).to eq(17)
|
26
|
-
expect("eighteen" .in_numbers).to eq(18)
|
27
|
-
expect("nineteen" .in_numbers).to eq(19)
|
28
|
-
expect("twenty" .in_numbers).to eq(20)
|
46
|
+
it 'should handle hundreds' do
|
47
|
+
expect('one hundred'.in_numbers).to eq(100)
|
48
|
+
expect('two hundred'.in_numbers).to eq(200)
|
49
|
+
expect('three hundred'.in_numbers).to eq(300)
|
50
|
+
expect('nine hundred'.in_numbers).to eq(900)
|
51
|
+
expect('one hundred and seventy six'.in_numbers).to eq(176)
|
52
|
+
expect('one hundred and seventy nine'.in_numbers).to eq(179)
|
53
|
+
expect('nine hundred and ninety nine'.in_numbers).to eq(999)
|
29
54
|
end
|
30
55
|
|
31
|
-
it
|
32
|
-
expect(
|
33
|
-
expect(
|
34
|
-
expect(
|
35
|
-
expect(
|
36
|
-
expect(
|
37
|
-
|
38
|
-
|
39
|
-
expect(
|
40
|
-
expect(
|
41
|
-
expect(
|
56
|
+
it 'should handle unusual hundreds' do
|
57
|
+
expect('eleven hundred'.in_numbers).to eq(1100)
|
58
|
+
expect('twelve hundred'.in_numbers).to eq(1200)
|
59
|
+
expect('thirteen hundred'.in_numbers).to eq(1300)
|
60
|
+
expect('fifteen hundred'.in_numbers).to eq(1500)
|
61
|
+
expect('nineteen hundred'.in_numbers).to eq(1900)
|
62
|
+
end
|
63
|
+
it 'should handle thousands' do
|
64
|
+
expect('two thousand and one'.in_numbers).to eq(2001)
|
65
|
+
expect('one thousand'.in_numbers).to eq(1000)
|
66
|
+
expect('two thousand'.in_numbers).to eq(2000)
|
67
|
+
expect('three thousand'.in_numbers).to eq(3000)
|
68
|
+
expect('nine thousand'.in_numbers).to eq(9000)
|
69
|
+
expect('nine thousand two hundred'.in_numbers).to eq(9200)
|
70
|
+
expect('nine thousand two hundred and seven'.in_numbers).to eq(9207)
|
71
|
+
expect('nine thousand two hundred and ninety seven'.in_numbers).to eq(9297)
|
42
72
|
end
|
43
73
|
|
44
|
-
it
|
45
|
-
expect(
|
46
|
-
expect(
|
47
|
-
expect("three hundred" .in_numbers).to eq(300)
|
48
|
-
expect("nine hundred" .in_numbers).to eq(900)
|
49
|
-
expect("one hundred and seventy six" .in_numbers).to eq(176)
|
50
|
-
expect("one hundred and seventy nine" .in_numbers).to eq(179)
|
51
|
-
expect("nine hundred and ninety nine" .in_numbers).to eq(999)
|
74
|
+
it 'handles numbers that begin with a instead of one', :aggregate_failures do
|
75
|
+
expect('a thousand six hundred'.in_numbers).to eq(1600)
|
76
|
+
expect('a thousand six hundred and fifty-five'.in_numbers).to eq(1655)
|
52
77
|
end
|
53
78
|
|
54
|
-
it
|
55
|
-
expect(
|
56
|
-
expect(
|
57
|
-
expect(
|
58
|
-
expect(
|
59
|
-
|
79
|
+
it 'should handle larger numbers' do
|
80
|
+
expect('one million'.in_numbers).to eq(1_000_000)
|
81
|
+
expect('two googol five billion and seventy six'.in_numbers).to eq(2 * 10**100 + 5 * 10**9 + 76)
|
82
|
+
expect('thirty seven million'.in_numbers).to eq(37 * 10**6)
|
83
|
+
expect('twenty six googol'.in_numbers).to eq(26 * 10**100)
|
84
|
+
end
|
60
85
|
|
86
|
+
it 'should handle numbers in hundreds of thousands etc' do
|
87
|
+
expect('nine hundred thousand'.in_numbers).to eq(900_000)
|
88
|
+
expect('three hundred and fifty seven thousand'.in_numbers).to eq(357_000)
|
89
|
+
expect('five million three hundred and fifty seven thousand'.in_numbers).to eq(5_357_000)
|
90
|
+
expect('nine hundred and ninety nine trillion'.in_numbers).to eq(999 * 10**12)
|
91
|
+
end
|
92
|
+
it 'should handle negative numbers' do
|
93
|
+
expect('minus one'.in_numbers).to eq(-1)
|
94
|
+
expect('minus two googol'.in_numbers).to eq(-2 * 10**100)
|
95
|
+
expect('minus nine hundred and ninety nine trillion'.in_numbers).to eq(-999 * 10**12)
|
61
96
|
end
|
62
|
-
|
63
|
-
|
64
|
-
expect(
|
65
|
-
expect(
|
66
|
-
expect("three
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
expect(
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
it "should handle decimal points" do
|
100
|
-
expect("one point one" .in_numbers) .to eq(1.1)
|
101
|
-
|
102
|
-
expect("zero point seven six five three four" .in_numbers) .to eq(0.76534)
|
103
|
-
expect("one trillion point six" .in_numbers) .to eq(10**12 + 0.6)
|
104
|
-
|
105
|
-
long_number = <<-NUMBER
|
106
|
-
nine duotrigintillion seven hundred and seventy seven untrigintillion
|
107
|
-
fifty nine trigintillion one hundred and sixty novemvigintillion
|
108
|
-
eight hundred and six octovigintillion seven hundred and thirty six
|
109
|
-
septenvigintillion four hundred and seventy one sexvigintillion
|
110
|
-
nine hundred and seventy quinvigintillion six hundred and thirty two
|
111
|
-
quattuorvigintillion eight hundred and twenty seven trevigintillion
|
112
|
-
eight hundred and thirty six duovigintillion nine hundred and fifty
|
113
|
-
two unvigintillion seven hundred and ten vigintillion eight hundred and one
|
114
|
-
novemdecillion nine hundred and forty eight octodecillion seven hundred
|
115
|
-
and five septendecillion six hundred and eighty three sexdecillion
|
116
|
-
one hundred and six quindecillion seven hundred and seven quattuordecillion
|
117
|
-
seven hundred and fifty seven tredecillion four hundred and twenty six
|
118
|
-
duodecillion seven hundred and ninety five undecillion seven hundred
|
119
|
-
and forty six decillion eight hundred and thirteen nonillion one hundred
|
120
|
-
and twenty seven octillion four hundred and sixty five septillion two
|
121
|
-
hundred and thirty seven sextillion one hundred and thirty nine
|
122
|
-
quintillion one hundred and fifty three quadrillion forty six
|
123
|
-
trillion seven hundred and fifty two billion eight hundred and three
|
124
|
-
million ninety three thousand seven hundred and ninety one point
|
125
|
-
eight nine five six four three two one eight nine five six seven eight
|
97
|
+
|
98
|
+
it 'should ignore punctuation and capitalisation' do
|
99
|
+
expect('Minus one'.in_numbers).to eq(-1)
|
100
|
+
expect('FIVE Million, three hundred and fifty-seVen Thousand'.in_numbers).to eq(5_357_000)
|
101
|
+
expect("FIVE,,./';';';[] Million, three hundred and fifty-seVen Thousand".in_numbers).to eq(5_357_000)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should handle decimal points' do # rubocop: disable Metrics/BlockLength
|
105
|
+
expect('one point one'.in_numbers).to eq(1.1)
|
106
|
+
|
107
|
+
expect('zero point seven six five three four'.in_numbers).to eq(0.76534)
|
108
|
+
expect('one trillion point six'.in_numbers).to eq(10**12 + 0.6)
|
109
|
+
|
110
|
+
long_number = <<~NUMBER
|
111
|
+
nine duotrigintillion seven hundred and seventy seven untrigintillion
|
112
|
+
fifty nine trigintillion one hundred and sixty novemvigintillion
|
113
|
+
eight hundred and six octovigintillion seven hundred and thirty six
|
114
|
+
septenvigintillion four hundred and seventy one sexvigintillion
|
115
|
+
nine hundred and seventy quinvigintillion six hundred and thirty two
|
116
|
+
quattuorvigintillion eight hundred and twenty seven trevigintillion
|
117
|
+
eight hundred and thirty six duovigintillion nine hundred and fifty
|
118
|
+
two unvigintillion seven hundred and ten vigintillion eight hundred and one
|
119
|
+
novemdecillion nine hundred and forty eight octodecillion seven hundred
|
120
|
+
and five septendecillion six hundred and eighty three sexdecillion
|
121
|
+
one hundred and six quindecillion seven hundred and seven quattuordecillion
|
122
|
+
seven hundred and fifty seven tredecillion four hundred and twenty six
|
123
|
+
duodecillion seven hundred and ninety five undecillion seven hundred
|
124
|
+
and forty six decillion eight hundred and thirteen nonillion one hundred
|
125
|
+
and twenty seven octillion four hundred and sixty five septillion two
|
126
|
+
hundred and thirty seven sextillion one hundred and thirty nine
|
127
|
+
quintillion one hundred and fifty three quadrillion forty six
|
128
|
+
trillion seven hundred and fifty two billion eight hundred and three
|
129
|
+
million ninety three thousand seven hundred and ninety one point
|
130
|
+
eight nine five six four three two one eight nine five six seven eight
|
126
131
|
NUMBER
|
132
|
+
|
133
|
+
# rubocop: disable Metrics/LineLength
|
127
134
|
expect(long_number.in_numbers).to eq(
|
128
|
-
|
135
|
+
9_777_059_160_806_736_471_970_632_827_836_952_710_801_948_705_683_106_707_757_426_795_746_813_127_465_237_139_153_046_752_803_093_791.89564321895678
|
129
136
|
)
|
137
|
+
# rubocop: enable Metrics/LineLength
|
130
138
|
|
131
|
-
expect(
|
132
|
-
|
139
|
+
expect('seventy five point eight four three two seven six nine four five one eight'
|
140
|
+
.in_numbers).to eq(75.84327694518)
|
133
141
|
end
|
134
142
|
|
143
|
+
it 'should handle years notation' do
|
144
|
+
expect('fifteen sixteen'.in_numbers).to eq(1516)
|
145
|
+
expect('eighty five sixteen'.in_numbers).to eq(8516)
|
146
|
+
expect('nineteen ninety six'.in_numbers).to eq(1996)
|
147
|
+
expect('forty nine ninety eight forty seven seventy nine'.in_numbers).to eq(49_984_779)
|
148
|
+
expect('fifteen sixteen'.in_numbers).to eq(1516)
|
149
|
+
expect('fifteen sixteen seven'.in_numbers).to eq(15_167)
|
150
|
+
expect('fifteen sixteen seventeen'.in_numbers).to eq(151_617)
|
151
|
+
end
|
135
152
|
end
|