Created
May 17, 2011 13:34
-
-
Save LTe/976471 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def number_in_words(n) | |
return '' if n == 0 | |
sc = [''] + %w{jeden dwa trzy cztery pięć sześć siedem osiem dziewięć} | |
sn = %w{dziesięć jedenaście dwanaście trzynaście czternaście piętnaście szesnaście siedemnaście osiemnaście dziewiętnaście} | |
sd = ['',''] + %w{dwadzieścia trzydzieści czterdzieści pięćdziesiąt sześćdziesiąt siedemdziesiąt osiemdziesiąt dziewiędziesiąt sto} | |
ss = [''] + %w{sto dwieście trzysta czterysta pięćset sześćset siedemset osiemset dziewięćset} | |
b = ['','',''],%w{tysiąc tysiące tysięcy},%w{milion miliony milionów},%w{miliard miliardy miliardów} | |
p = n.to_s.size | |
return 'bardzo dużo' if p > 11 | |
d,dn = n.to_s[0,(p%3 == 0 ? 3 : p%3)], n.to_s[(p%3 == 0 ? 3 : p%3)..-1] | |
return "#{d.to_i==0 ? '' : b[((p-1)/3.0).floor][0]} #{number_in_words(dn)}".strip if (d.to_i == 1 or d.to_i == 0 ) and n != 1 | |
r = '' | |
(d.size-1).downto(0) do |i| | |
r += ' ' | |
r += ss[d[-i-1].chr.to_i] if i == 2 | |
d[-i-1].chr.to_i == 1 ? (r += sn[d[-i].chr.to_i]; d = d[0..-2]; break) : r += sd[d[-i-1].chr.to_i] if i == 1 | |
r += sc[d[-i-1].chr.to_i] if i == 0 | |
end | |
(2..4) === (d.to_i % 10) ? k=1 : k=2 | |
"#{r} #{b[((p-1)/3.0).floor][k]} #{number_in_words(dn.to_i)}".strip | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment