Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Napravljena do kraja, Readme, test prebacen u poseban fajl
- Loading branch information
Showing
3 changed files
with
133 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
= Show me the money - croatian currency to words | ||
This library konverts "kuna" and "lipa" amounts to croatian currency | ||
|
||
Something like this: | ||
0.11 --> nula kuna jedanaest lipa | ||
1.44 --> jedna kuna i četrdeset četiri lipe | ||
213.12 --> dvjesto trinaest kuna i dvanaest lipa | ||
1234.56 --> jedna tisuća dvjesto trideset četiri kune i pedeset šest lipa | ||
123.22 --> sto dvadeset tri kune i dvadeset dvije lipe | ||
45353.33 --> četrdeset pet tisuća tristo pedeset tri kune i trideset tri lipe | ||
2209.12 --> dvije tisuće dvjesto devet kuna i dvanaest lipa | ||
3323.00 --> tri tisuće tristo dvadeset tri kune i nula lipa | ||
232445.88 --> dvjesto trideset dvije tisuće četristo četrdeset pet kuna i osamdeset osam lipa | ||
566321.00 --> petsto šezdeset šest tisuća tristo dvadeset jedna kuna i nula lipa | ||
90300.00 --> devedeset nula tisuće tristo nula kuna i nula lipa | ||
1134299.17 --> jedan milijun sto trideset četiri tisuće dvjesto devedeset devet kuna i sedamnaest lipa | ||
2998877.11 --> dva milijuna devetsto devedeset osam tisuća osamsto sedamdeset sedam kuna i jedanaest lipa | ||
2998877.12 --> dva milijuna devetsto devedeset osam tisuća osamsto sedamdeset sedam kuna i dvanaest lipa | ||
12998877.12 --> dvanaest milijuna devetsto devedeset osam tisuća osamsto sedamdeset sedam kuna i dvanaest lipa | ||
343526879.02 --> tristo četrdeset tri milijuna petsto dvadeset šest tisuća osamsto sedamdeset devet kuna i dvije lipe | ||
|
||
Yeah, it's not all good and needs fixin. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,127 @@ | ||
class ShowMeTheMoney | ||
JEDINICE = ['nula', 'jedna', 'dvije', 'tri', 'četiri', 'pet', | ||
'šest', 'sedam', 'osam', 'devet', 'deset', | ||
'jedanaest', 'dvanaest', 'trinaest' , 'četrnaest', | ||
'petnaest', 'šesnaest', 'sedamnaest', 'osamnaest', 'devetnaest'] | ||
# pretvara 2 iznosa (kune i lipe) u iznos riječima | ||
# Autor: Tomislav Car, Matej Špoler, Josip Bišćan | ||
# Infinum d.o.o., 2009 | ||
|
||
tmp = JEDINICE | ||
tmp[1] = 'jedan' | ||
tmp[2] = 'dva' | ||
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 = tmp # za miliune | ||
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'] | ||
POM_RIJECI = { | ||
'miliun'=>'miliun', | ||
'miliuna'=>'miliuna', | ||
'tisucu'=>'tisuću', | ||
'tisuce'=>'tisuće', | ||
'tisuca'=>'tisuća', | ||
'kuna'=>'kuna', | ||
'kune'=>'kune', | ||
'lipa'=>'lipa', | ||
'lipe'=>'lipe', | ||
'veznik'=>' i ' | ||
} | ||
|
||
# slaze cijenu slovima iz broja | ||
def number_to_string($number) | ||
$num_1 = substr($number, 0, -3) | ||
$num_2 = substr($number, -2) | ||
'šesto', 'sedamsto', 'osamsto', 'devetsto'] | ||
|
||
# ocistimo separatore u kunskom dijelu | ||
$num_1 = $num_1.gsub(".", '') # da nam ne razjebavaju sustav | ||
$num_1 = $num_1.gsub(",", '') | ||
|
||
$num_1_str = number_to_string_rek($num_1, 0).strip # kn | ||
$num_2_str = number_to_string_rek($num_2, 0) # lp | ||
|
||
|
||
$ret = $num_1_str + ' ' | ||
$num_1 = array_reverse(str_split($num_1)) | ||
$num_1_pom = $num_1[1] . $num_1[0] | ||
$num_2 = array_reverse(str_split($num_2)) | ||
$num_2_pom = $num_2[1] . $num_2[0] | ||
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 | ||
|
||
# kune | ||
if ($num_1[0]!=2 && $num_1[0]!=3 && $num_1[0]!=4 ) # brojevi koji zavrsavaju na 2,3,4 imaju 'kune', ostali 'kuna' | ||
$ret .= POM_RIJECI['kuna'] | ||
else | ||
$ret .= POM_RIJECI['kune'] | ||
end | ||
# 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 | ||
if ($ret!='') | ||
$ret .= POM_RIJECI['veznik'] | ||
end | ||
parts << VEZNIK if kune.to_i>0 | ||
|
||
# lipe | ||
if (($num_2[0]==2 || $num_2[0]==3 || $num_2[0]==4) && !($num_2_pom>11 && $num_2_pom<14)) # 2,3,4 lipe, ostalo lipa | ||
$ret .= $num_2_str . ' ' . POM_RIJECI['lipe'] | ||
else | ||
$ret .= $num_2_str . ' ' . POM_RIJECI['lipa'] | ||
end | ||
|
||
return $ret | ||
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_rek($number, $rek, $miliuni=0) | ||
$str = '' | ||
|
||
$num = str_split($number) | ||
$num = array_reverse($num); | ||
|
||
if ($number=='0' || $number=='00') #brojevi do 20 su specificni | ||
return JEDINICE[0] | ||
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 (!$rek) | ||
$num_tisucu = $num[5] . $num[4] . $num[3] | ||
$num_miliun = $num[8] . $num[7] . $num[6] | ||
if !thousands | ||
num_miliun = num[8].to_s + num[7].to_s + num[6].to_s | ||
|
||
if ($num_miliun>0) | ||
if ($num_miliun==1) | ||
$str .= ' ' . POM_RIJECI['miliun'] . ' '; | ||
elsif ($num[6]==1) | ||
$str .= number_to_string_rek($num_miliun, 1, 1) . ' ' . POM_RIJECI['miliun'] . ' ' | ||
else | ||
$str .= number_to_string_rek($num_miliun, 1, 1). ' ' . POM_RIJECI['miliuna'] . ' ' | ||
end | ||
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 | ||
|
||
if ($num_tisucu>0) | ||
if ($num_tisucu==1) | ||
$str .= ' ' . POM_RIJECI['tisucu'] . ' ' | ||
elsif ($num[3]==1) | ||
$str .= number_to_string_rek($num_tisucu, 1) . ' ' . POM_RIJECI['tisuca'] . ' ' | ||
elsif ($num[3]<5) | ||
$str .= number_to_string_rek($num_tisucu, 1) . ' ' . POM_RIJECI['tisuce'] . ' ' | ||
else | ||
$str .= number_to_string_rek($num_tisucu, 1). ' ' . POM_RIJECI['tisuca'] . ' ' | ||
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 | ||
|
||
if ($num[2]) # stotice | ||
$str .= STOTICE[$num[2]] . ' ' | ||
if ($num[1]) # desetice | ||
$str .= DESETICE[$num[1]] . ' ' | ||
if ($num[1]==1) # brojevi od 10 do 20 (stotice ne racunamo) | ||
if ($miliuni) | ||
$str .= JEDINICE_MUSKE[$num[1] . $num[0]] | ||
else | ||
$str .= JEDINICE[$num[1] . $num[0]] | ||
end | ||
elsif ($num[0]) # jedinice, s time da su desetice ili 0 ili vece od 1 | ||
if ($miliuni) | ||
$str .= JEDINICE_MUSKE[$num[0]] | ||
else | ||
$str .= JEDINICE[$num[0]] | ||
end | ||
end | ||
parts << STOTICE[num[2].to_i] if num[2] # stotice | ||
parts << DESETICE[num[1].to_i] if num[1] # desetice | ||
|
||
# u nekim okolnostima moze doci do dvostrukog razmaka pa da to maknem | ||
$str = $str.gsub(' ', ' ') | ||
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 | ||
|
||
return $str | ||
parts | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'show_me_the_money' | ||
|
||
# yeah, this is not a real test | ||
["0.11", "1.44", "213.12", "1234.56", "123.22", "45353.33", "2209.12", "3323.00", | ||
"232445.88", "566321.00", "90300.00", | ||
"1134299.17", "2998877.11", "2998877.12", "12998877.12", "343526879.02"].each do |amount| | ||
arr = amount.split(".") | ||
kune, lipe = arr[0], arr[1] | ||
puts amount.to_s + " --> " + ShowMeTheMoney.new.kune_in_words(kune, lipe) | ||
end | ||
|