number_to_text 0.1.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: 31ce26201cbcefe7811fce61397b0166c60f3119
4
+ data.tar.gz: 994bd68e5a094686681d38a0b422359c45aab33c
5
+ SHA512:
6
+ metadata.gz: f3dc101def956cfc88d9efdea5672a43afaf3754228b309adbf907fdd8211ca38db6717f4815644cb43ef11f2d8ae5204955bce75f1b40eef91014db72fbca32
7
+ data.tar.gz: 2a3184c9a1842427aa713151a071d9b4290a4b838d5c2b4d7f27f652dd51cf09d6ed1b670a245d5c103bf7afde196e82316a9959bb47c098faf17aa4661c1156
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Jan Habermann
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # NumberToText
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/number_to_text`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'number_to_text'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install number_to_text
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/number_to_text/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1,18 @@
1
+ require 'number_to_text/version'
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+
6
+ module NumberToText
7
+ extend ActiveSupport::Autoload
8
+
9
+ eager_autoload do
10
+ autoload :NumberToTextConverter
11
+ end
12
+
13
+ module_function
14
+
15
+ def number_to_text(number, options = {})
16
+ NumberToTextConverter.convert(number, options)
17
+ end
18
+ end
@@ -0,0 +1,123 @@
1
+ require 'active_support/number_helper'
2
+
3
+ module NumberToText
4
+ include ActiveSupport::NumberHelper
5
+
6
+ class NumberToTextConverter < NumberConverter
7
+ def convert # :nodoc:
8
+ @number = Integer(number)
9
+ digits = 0
10
+ result = ''
11
+ negative = number < 0
12
+ @number = @number.abs
13
+
14
+ return 'null' if number == 0
15
+
16
+ while @number > 0
17
+ if @number % 1000 > 0
18
+ result = [to_text_1000(number % 1000, digits), digits_to_text(digits, @number % 1000 > 1), result].join
19
+ end
20
+ @number /= 1000
21
+ digits += 3
22
+ end
23
+
24
+ result = 'minus ' + result if negative
25
+
26
+ result.strip
27
+ end
28
+
29
+ private
30
+ def to_text_10(number)
31
+ number = number.to_i / 10
32
+ case number
33
+ when 1 then 'zehn'
34
+ when 2 then 'zwanzig'
35
+ when 3 then 'dreißig'
36
+ when 4 then 'vierzig'
37
+ when 5 then 'fünfzig'
38
+ when 6 then 'sechzig'
39
+ when 7 then 'siebzig'
40
+ when 8 then 'achtzig'
41
+ when 9 then 'neunzig'
42
+ end
43
+ end
44
+
45
+ def to_text_100(number, digits)
46
+ number = number % 100
47
+
48
+ if number == 1
49
+ case digits
50
+ when 0
51
+ return 'eins'
52
+ when 1..3
53
+ return 'ein'
54
+ when 6, 9, 12, 15
55
+ if number == 1
56
+ return 'eine'
57
+ else
58
+ return 'ein'
59
+ end
60
+ end
61
+ end
62
+
63
+ if number.between?(2, 19)
64
+ case number
65
+ when 2 then return 'zwei'
66
+ when 3 then return 'drei'
67
+ when 4 then return 'vier'
68
+ when 5 then return 'fünf'
69
+ when 6 then return 'sechs'
70
+ when 7 then return 'sieben'
71
+ when 8 then return 'acht'
72
+ when 9 then return 'neun'
73
+ when 10 then return 'zehn'
74
+ when 11 then return 'elf'
75
+ when 12 then return 'zwölf'
76
+ when 13 then return 'dreizehn'
77
+ when 14 then return 'vierzehn'
78
+ when 15 then return 'fünfzehn'
79
+ when 16 then return 'sechzehn'
80
+ when 17 then return 'siebzehn'
81
+ when 18 then return 'achtzehn'
82
+ when 19 then return 'neunzehn'
83
+ end
84
+ elsif number.between?(20, 99)
85
+ if number % 10 == 0
86
+ to_text_10(number).to_s
87
+ else
88
+ to_text_100(number % 10, 1).to_s + 'und' + to_text_10(number).to_s
89
+ end
90
+ end
91
+ end
92
+
93
+ def to_text_1000(number, digits)
94
+ if number / 100 == 0
95
+ to_text_100(number, digits).to_s
96
+ else
97
+ to_text_100(number / 100, 2).to_s + 'hundert' + to_text_100(number, digits).to_s
98
+ end
99
+ end
100
+
101
+ def digits_to_text(digits, plural)
102
+ if plural
103
+ case digits
104
+ when 0 then ''
105
+ when 3 then 'tausend'
106
+ when 6 then ' Millionen '
107
+ when 9 then ' Milliarden '
108
+ when 12 then ' Billionen '
109
+ when 15 then ' Billiarden '
110
+ end
111
+ else
112
+ case digits
113
+ when 0 then ''
114
+ when 3 then 'tausend'
115
+ when 6 then ' Million '
116
+ when 9 then ' Milliarde '
117
+ when 12 then ' Billion '
118
+ when 15 then ' Billiarde '
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,3 @@
1
+ module NumberToText
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: number_to_text
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ann-Christin Preuß
8
+ - Jan Habermann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 4.1.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 4.1.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: A simple helper that converts numbers to text
43
+ email:
44
+ - jan@habermann24.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - lib/number_to_text.rb
52
+ - lib/number_to_text/number_to_text_converter.rb
53
+ - lib/number_to_text/version.rb
54
+ homepage: https://github.com/habermann24/number_to_text
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.9.3
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.4.5
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Convert numbers to spoken text
78
+ test_files: []