numerale 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/numerale/.DS_Store +0 -0
- data/lib/numerale/version.rb +3 -0
- data/lib/numerale.rb +149 -0
- data/numerale.gemspec +22 -0
- metadata +87 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
Binary file
|
data/lib/numerale.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
class Numerale
|
2
|
+
UNITS = {0 => "zero",
|
3
|
+
1 => "jeden",
|
4
|
+
2 => "dwa",
|
5
|
+
3 => "trzy",
|
6
|
+
4 => "cztery",
|
7
|
+
5 => "pięć",
|
8
|
+
6 => "sześć",
|
9
|
+
7 => "siedem",
|
10
|
+
8 => "osiem",
|
11
|
+
9 => "dziewięć",
|
12
|
+
10 => "dziesięć",
|
13
|
+
11 => "jedenaście",
|
14
|
+
12 => "dwanaście",
|
15
|
+
13 => "trzynaście",
|
16
|
+
14 => "czternaście",
|
17
|
+
15 => "piętnaście",
|
18
|
+
16 => "szesnaście",
|
19
|
+
17 => "siedemnaście",
|
20
|
+
18 => "osiemnaście",
|
21
|
+
19 => "dziewiętnaście",
|
22
|
+
20 => "dwadzieścia",
|
23
|
+
30 => 'trzydzieści',
|
24
|
+
40 => 'czterdzieści',
|
25
|
+
50 => 'pięćdziesiąt',
|
26
|
+
60 => 'sześćdziesiąt',
|
27
|
+
70 => 'siedemdziesiąt',
|
28
|
+
80 => 'osiemdziesiąt',
|
29
|
+
90 => 'dziewięćdziesiąt',
|
30
|
+
100 => "sto",
|
31
|
+
200 => "dwieście",
|
32
|
+
300 => "trzysta",
|
33
|
+
400 => "czterysta",
|
34
|
+
500 => "pięćset",
|
35
|
+
600 => 'sześćset',
|
36
|
+
700 => 'siedemset',
|
37
|
+
800 => 'osiemset',
|
38
|
+
900 => 'dziewięćset',
|
39
|
+
1000 => {1 => 'tysiąc', 2 => 'tysiące', 5 => 'tysięcy'},
|
40
|
+
1000000 => {1 => 'milion', 2 => 'miliony', 5 => 'milionów'},
|
41
|
+
1000000000 => {1 => 'miliard', 2 => 'miliardy', 5 => 'miliardów'}}
|
42
|
+
|
43
|
+
CURRENCIES = {
|
44
|
+
'PLN' => { 'full' => {1 => 'złoty', 2 => 'złote', 5 => 'złotych'}, 'frac' => {1 => 'grosz', 2 => 'grosze', 5 => 'groszy'} },
|
45
|
+
'USD' => { 'full' => {1 => 'dollar', 2 => 'dollars', 5 => 'dollars'}, 'frac' => {1 => 'cent', 2 => 'cents', 5 => 'cents'}}
|
46
|
+
}
|
47
|
+
|
48
|
+
|
49
|
+
def initialize(d)
|
50
|
+
number = d
|
51
|
+
precision = 10**2
|
52
|
+
number = [number.to_i, ((number * precision).round % precision).to_i]
|
53
|
+
|
54
|
+
decomposition = []
|
55
|
+
i = 0
|
56
|
+
number.each do |n|
|
57
|
+
decomposition[i] = {}
|
58
|
+
|
59
|
+
if n == 0
|
60
|
+
decomposition[i][1] = 0
|
61
|
+
else
|
62
|
+
pow = 10**9
|
63
|
+
count = n
|
64
|
+
|
65
|
+
while pow > 0 do
|
66
|
+
decomposition[i][pow] = count / pow
|
67
|
+
count %= pow
|
68
|
+
pow /= 10**3
|
69
|
+
end
|
70
|
+
end
|
71
|
+
i += 1
|
72
|
+
end
|
73
|
+
|
74
|
+
@number = decomposition
|
75
|
+
end
|
76
|
+
|
77
|
+
def expand_partial(number, pow)
|
78
|
+
result = ""
|
79
|
+
@ones = 0
|
80
|
+
|
81
|
+
if number.include?(pow) && number[pow] > 0
|
82
|
+
hundreds = (number[pow] / 10**2) * 10**2
|
83
|
+
if (number[pow] % 10**2) < 20 && ((number[pow] % 10**2) > 10)
|
84
|
+
tens = (number[pow] % 10**2)
|
85
|
+
ones = 0
|
86
|
+
else
|
87
|
+
tens = ((number[pow] % 10**2) / 10) * 10
|
88
|
+
ones = number[pow] % 10
|
89
|
+
end
|
90
|
+
|
91
|
+
result += "#{UNITS[hundreds]} " if hundreds > 0
|
92
|
+
result += "#{UNITS[tens]} " if tens > 0
|
93
|
+
result += "#{UNITS[ones]} " if ones > 0
|
94
|
+
|
95
|
+
c = (hundreds > 0 || tens > 0) ? ones > 1 && ones < 5 ? 2 : 5 : ones == 1 ? 1 : ones < 5 ? 2 : 5
|
96
|
+
result += "#{UNITS[pow][c]} " if UNITS[pow].class == Hash
|
97
|
+
|
98
|
+
@hundreds = hundreds
|
99
|
+
@tens = tens
|
100
|
+
@ones = ones
|
101
|
+
end
|
102
|
+
|
103
|
+
return result
|
104
|
+
end
|
105
|
+
|
106
|
+
def expand(number, currency, ending)
|
107
|
+
result = ""
|
108
|
+
|
109
|
+
pow = 10**9
|
110
|
+
ones = 0
|
111
|
+
|
112
|
+
while pow > 0
|
113
|
+
result += expand_partial(number, pow)
|
114
|
+
pow /= 10**3
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
if result.strip == "" && number[1] == 0
|
119
|
+
result += "#{UNITS[0]} "
|
120
|
+
end
|
121
|
+
|
122
|
+
c = @hundreds > 0 || @tens > 0 ? @ones > 1 && @ones < 5 ? 2 : 5 : @ones == 1 ? 1 : @ones < 5 ? 2 : 5
|
123
|
+
result += "#{CURRENCIES[currency][ending][c]}" if result.strip != ""
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def to_text(currency = "PLN")
|
128
|
+
result = ""
|
129
|
+
result = "#{expand(@number[0], currency, 'full')}"
|
130
|
+
result += " " if result.strip != ""
|
131
|
+
result += "#{expand(@number[1], currency, 'frac')}"
|
132
|
+
puts result
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
class Float
|
138
|
+
def to_text(currency = "PLN")
|
139
|
+
c = Numerale.new(self)
|
140
|
+
c.to_text(currency)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class Fixnum
|
145
|
+
def to_text(currency = "PLN")
|
146
|
+
c = Numerale.new(self)
|
147
|
+
c.to_text(currency)
|
148
|
+
end
|
149
|
+
end
|
data/numerale.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "numerale/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "numerale"
|
7
|
+
s.version = Numerale::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Piotr Debosz"]
|
10
|
+
s.email = ["piotr.debosz@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Gem allows change numerals to words}
|
13
|
+
s.description = %q{Gem allows change numerals to words (only in Polish, yet)}
|
14
|
+
|
15
|
+
s.rubyforge_project = "numerale"
|
16
|
+
s.add_development_dependency "rspec"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: numerale
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Piotr Debosz
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-12 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Gem allows change numerals to words (only in Polish, yet)
|
36
|
+
email:
|
37
|
+
- piotr.debosz@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- Rakefile
|
48
|
+
- lib/numerale.rb
|
49
|
+
- lib/numerale/.DS_Store
|
50
|
+
- lib/numerale/version.rb
|
51
|
+
- numerale.gemspec
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: ""
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: numerale
|
82
|
+
rubygems_version: 1.6.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Gem allows change numerals to words
|
86
|
+
test_files: []
|
87
|
+
|