polish-number 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -2
- data/lib/polish_number.rb +49 -37
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -2,19 +2,23 @@
|
|
2
2
|
Translates numbers to Polish words
|
3
3
|
|
4
4
|
= Installation
|
5
|
+
Tested with Ruby 1.8.7 and 1.9.2.
|
6
|
+
|
5
7
|
gem install polish-number
|
6
8
|
|
7
9
|
= Usage
|
8
10
|
require 'polish_number'
|
9
|
-
|
11
|
+
|
10
12
|
PolishNumber.translate(1234) #=> tysiąc dwieście trzydzieści cztery
|
13
|
+
PolishNumber.translate(34, :currency => :PLN) #=> trzydzieści cztery złote
|
14
|
+
PolishNumber.translate(12, :currency => :PLN) #=> dwanaście złotych
|
11
15
|
|
12
16
|
= Running specs
|
13
17
|
|
14
18
|
rake test
|
15
19
|
|
16
20
|
= TODO
|
17
|
-
- currently works for
|
21
|
+
- currently works for numbers 0-999999. Make it work with millions, billions, etc
|
18
22
|
- optional noun pluralization
|
19
23
|
|
20
24
|
= GitHub
|
data/lib/polish_number.rb
CHANGED
@@ -15,35 +15,43 @@ module PolishNumber
|
|
15
15
|
|
16
16
|
ZERO = 'zero'
|
17
17
|
|
18
|
-
THOUSANDS =
|
18
|
+
THOUSANDS = {:one => 'tysiąc', :few => 'tysiące', :many => 'tysięcy'}
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
CURRENCIES = {
|
21
|
+
:PLN => {:one => 'złoty', :few => 'złote', :many => 'złotych'}
|
22
|
+
}
|
22
23
|
|
23
|
-
def self.translate(number)
|
24
|
-
|
25
|
-
|
26
|
-
unless (0..999999.99).include? number
|
27
|
-
raise ArgumentError, 'number should be in 0..999999.99 range'
|
24
|
+
def self.translate(number, options={})
|
25
|
+
if options[:currency] && !CURRENCIES.has_key?(options[:currency])
|
26
|
+
raise ArgumentError, "unknown :currency option '#{options[:currency].inspect}'. Choose one from: #{CURRENCIES.inspect}"
|
28
27
|
end
|
29
28
|
|
30
|
-
|
29
|
+
number = number.to_i
|
31
30
|
|
32
|
-
|
33
|
-
|
31
|
+
unless (0..999999).include? number
|
32
|
+
raise ArgumentError, 'number should be in 0..999999 range'
|
33
|
+
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
result <<
|
43
|
-
result <<
|
44
|
-
result <<
|
35
|
+
if number == 0
|
36
|
+
result = ZERO.dup
|
37
|
+
else
|
38
|
+
formatted_number = sprintf('%06.0f', number)
|
39
|
+
digits = formatted_number.chars.map { |char| char.to_i }
|
40
|
+
|
41
|
+
result = ''
|
42
|
+
result << process_0_999(digits[0..2])
|
43
|
+
result << thousands(number/1000, digits[0..2])
|
44
|
+
result << ' '
|
45
|
+
result << process_0_999(digits[3..5])
|
46
|
+
result.strip!
|
47
|
+
end
|
48
|
+
|
49
|
+
if options[:currency]
|
50
|
+
currency = CURRENCIES[options[:currency]]
|
51
|
+
result << ' '
|
52
|
+
result << currency[classify(number, digits)]
|
45
53
|
end
|
46
|
-
|
54
|
+
|
47
55
|
result
|
48
56
|
end
|
49
57
|
|
@@ -52,29 +60,33 @@ module PolishNumber
|
|
52
60
|
def self.process_0_999(digits)
|
53
61
|
result = ''
|
54
62
|
result << HUNDREDS[digits[0]]
|
55
|
-
|
63
|
+
|
64
|
+
if digits[1] == 1 && digits[2] != 0
|
65
|
+
result << TEENS[digits[2]]
|
66
|
+
else
|
67
|
+
result << TENS[digits[1]]
|
68
|
+
result << UNITIES[digits[2]]
|
69
|
+
end
|
70
|
+
|
56
71
|
result
|
57
72
|
end
|
58
73
|
|
59
|
-
def self.
|
60
|
-
if
|
61
|
-
|
74
|
+
def self.thousands(number, digits)
|
75
|
+
if number == 0
|
76
|
+
''
|
62
77
|
else
|
63
|
-
|
78
|
+
THOUSANDS[classify(number, digits)]
|
64
79
|
end
|
65
80
|
end
|
66
81
|
|
67
|
-
def self.
|
68
|
-
if
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
thousand_id = 2
|
82
|
+
def self.classify(number, digits)
|
83
|
+
if number == 1
|
84
|
+
:one
|
85
|
+
# all numbers with 2, 3 or 4 at the end, but not teens
|
86
|
+
elsif digits && (2..4).include?(digits[-1]) && digits[-2] != 1
|
87
|
+
:few
|
74
88
|
else
|
75
|
-
|
89
|
+
:many
|
76
90
|
end
|
77
|
-
words[thousand_id]
|
78
91
|
end
|
79
92
|
end
|
80
|
-
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Wojciech Piekutowski
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-10-04 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|