/
show_me_the_money.rb
127 lines (104 loc) · 3.35 KB
/
show_me_the_money.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# pretvara 2 iznosa (kune i lipe) u iznos riječima
# Autor: Tomislav Car, Matej Špoler, Josip Bišćan
# Infinum d.o.o., 2009
class ShowMeTheMoney
JEDINICE = [
'nula', 'jedna', 'dvije', 'tri', 'četiri', 'pet',
'šest', 'sedam', 'osam', 'devet', 'deset',
'jedanaest', 'dvanaest', 'trinaest' , 'četrnaest',
'petnaest', 'šesnaest', 'sedamnaest', 'osamnaest', 'devetnaest'
]
JEDINICE_MUSKE = JEDINICE.map{|x| # za miliune
case x
when 'jedna'
'jedan'
when 'dvije'
'dva'
else
x
end
}
DESETICE = ['', '', 'dvadeset', 'trideset', 'četrdeset', 'pedeset',
'šezdeset', 'sedamdeset', 'osamdeset', 'devedeset']
STOTICE = ['', 'sto', 'dvjesto', 'tristo', 'četristo', 'petsto',
'šesto', 'sedamsto', 'osamsto', 'devetsto']
RIJECI = {
:milijun => {
:one => 'milijun',
:many => 'milijuna'
},
:tisucu => {
:one => 'tisuću',
:few => 'tisuće',
:many => 'tisuća'
},
:kuna => {
:one => 'kuna',
:few => 'kune', # nije skroz tocno zbog 22 kune, 23 kune ali ovdje prolazi
:many => 'kuna'
},
:lipa => {
:one => 'lipa',
:few => 'lipe',
:many => 'lipa'
}
}
VEZNIK = 'i'
SEPARATOR = ' ' # FIXME ne radi ako je ovo prazan string
# slaze cijenu slovima iz broja
def kune_in_words(kune, lipe)
parts = []
parts << number_to_string(kune).join(SEPARATOR).strip
parts << RIJECI[:kuna][quantify_amount(kune)]
# dodaj veznik
parts << VEZNIK if kune.to_i>0
parts << number_to_string(lipe).join(SEPARATOR).strip
parts << RIJECI[:lipa][quantify_amount(lipe)]
# u nekim okolnostima moze doci do dvostrukog separatora pa da to maknem
parts.join(SEPARATOR).gsub(SEPARATOR*2, ' ')
end
def quantify_amount(amount)
(2..4).include?(amount.to_i % 10) && !(12..14).include?(amount.to_i % 100) ? :few : :many
end
# slaze od broja njegov tekstualni oblik
# rekurzivno se poziva za tisucice i miliune
def number_to_string(number, thousands = false, millions = false)
# brojevi do 20 su specificni
return [JEDINICE[0]] if number=='0' || number=='00'
num = number.split("").reverse
parts = []
if !thousands
num_miliun = num[8].to_s + num[7].to_s + num[6].to_s
if num_miliun!=""
quantifier = num_miliun=="1" ? :one : :many
parts += number_to_string(num_miliun, true, true) if num_miliun!=1
parts << RIJECI[:milijun][quantifier]
end
num_tisucu = num[5].to_s + num[4].to_s + num[3].to_s
if (num_tisucu.to_i > 0)
parts += number_to_string(num_tisucu, true) if (num_tisucu.to_i != 1)
if (num_tisucu.to_i == 1)
quantifier = :one
else
if (num[3].to_i == 1)
quantifier = :many
elsif (num[3].to_i < 5)
quantifier = :few
else
quantifier = :many
end
end
parts << RIJECI[:tisucu][quantifier]
end
end
parts << STOTICE[num[2].to_i] if num[2] # stotice
parts << DESETICE[num[1].to_i] if num[1] # desetice
array = millions ? JEDINICE_MUSKE : JEDINICE
if num[1].to_i==1 # brojevi od 10 do 20 (stotice ne racunamo)
parts << array[(num[1] + num[0]).to_i]
elsif num[0] # jedinice, s time da su desetice ili 0 ili vece od 1
parts << array[num[0].to_i]
end
parts
end
end