Skip to content

Commit

Permalink
Merge pull request savoirfairelinux#58 from kszys/master
Browse files Browse the repository at this point in the history
Fixed issues with wrong text for currency in Polish language
  • Loading branch information
erozqba committed Feb 27, 2017
2 parents f29e792 + 7fdb613 commit 710a3b6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
20 changes: 13 additions & 7 deletions num2words/lang_PL.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
>>> print(n2w(12519.85))
dwanaście tysięcy pięćset dziewiętnaście przecinek osiemdziesiąt pięć
>>> print(n2w(123.50))
sto dwadzieścia trzy przecinek pięć
>>> print(fill(n2w(1234567890)))
miliard dwieście trzydzieści cztery miliony pięćset sześćdziesiąt
siedem tysięcy osiemset dziewięćdzisiąt
Expand Down Expand Up @@ -95,6 +98,9 @@
>>> print(to_currency(-1251985, cents = False))
minus dwanaście tysięcy pięćset dziewiętnaście euro, 85 centów
>>> print(to_currency(123.50, 'PLN', seperator=' i'))
sto dwadzieścia trzy złote i pięćdziesiąt groszy
"""
from __future__ import unicode_literals

Expand Down Expand Up @@ -178,7 +184,7 @@ def splitby3(n):
if start > 0:
yield int(n[:start])
for i in range(start, length, 3):
yield int(n[i:i+3])
yield int(n[i:i + 3])
else:
yield int(n)

Expand All @@ -188,7 +194,7 @@ def get_digits(n):


def pluralize(n, forms):
form = 0 if n==1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or n % 100 > 20)) else 2
form = 0 if n == 1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or n % 100 > 20)) else 2
return forms[form]


Expand All @@ -203,11 +209,9 @@ def int2word(n):
i -= 1
n1, n2, n3 = get_digits(x)

# print str(n3) + str(n2) + str(n1)

if n3 > 0:
words.append(HUNDREDS[n3][0])

if n2 > 1:
words.append(TWENTIES[n2][0])

Expand All @@ -231,20 +235,22 @@ def n2w(n):
return int2word(int(n))


def to_currency(n, currency='EUR', cents=True, seperator=','):
def to_currency(n, currency = 'EUR', cents = True, seperator = ','):
if type(n) == int:
if n < 0:
minus = True
else:
minus = False

n = abs(n)
left = n / 100
left = n // 100
right = n % 100
else:
n = str(n).replace(',', '.')
if '.' in n:
left, right = n.split('.')
if len(right) == 1:
right = right + '0'
else:
left, right = n, 0
left, right = int(left), int(right)
Expand Down
49 changes: 49 additions & 0 deletions tests/test_pl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- encoding: utf-8 -*-
# Copyright (c) 2015, Savoir-faire Linux inc. All Rights Reserved.

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA

from __future__ import unicode_literals

from unittest import TestCase

from num2words import num2words
from num2words.lang_PL import to_currency

class Num2WordsPLTest(TestCase):
def test_cardinal(self):
self.assertEqual(num2words(100, lang='pl'), "sto")
self.assertEqual(num2words(101, lang='pl'), "sto jeden")
self.assertEqual(num2words(110, lang='pl'), "sto dziesięć")
self.assertEqual(num2words(115, lang='pl'), "sto piętnaście")
self.assertEqual(num2words(123, lang='pl'), "sto dwadzieścia trzy")
self.assertEqual(num2words(1000, lang='pl'), "tysiąc")
self.assertEqual(num2words(1001, lang='pl'), "tysiąc jeden")
self.assertEqual(num2words(2012, lang='pl'), "dwa tysiące dwanaście")
self.assertEqual(num2words(12519.85, lang='pl'), "dwanaście tysięcy pięćset dziewiętnaście przecinek osiemdziesiąt pięć")
self.assertEqual(num2words(123.50, lang='pl'), "sto dwadzieścia trzy przecinek pięć")
self.assertEqual(num2words(1234567890, lang='pl'), "miliard dwieście trzydzieści cztery miliony pięćset sześćdziesiąt siedem tysięcy osiemset dziewięćdzisiąt")
self.assertEqual(num2words(215461407892039002157189883901676, lang='pl'), "dwieście piętnaście kwintylionów czterysta sześćdziesiąt jeden kwadryliardów czterysta siedem kwadrylionów osiemset dziewięćdzisiąt dwa tryliardy trzydzieści dziewięć trylionów dwa biliardy sto pięćdziesiąt siedem bilionów sto osiemdziesiąt dziewięć miliardów osiemset osiemdziesiąt trzy miliony dziewęćset jeden tysięcy sześćset siedemdziesiąt sześć")
self.assertEqual(num2words(719094234693663034822824384220291, lang='pl'), "siedemset dziewiętnaście kwintylionów dziewięćdzisiąt cztery kwadryliardy dwieście trzydzieści cztery kwadryliony sześćset dziewięćdzisiąt trzy tryliardy sześćset sześćdziesiąt trzy tryliony trzydzieści cztery biliardy osiemset dwadzieścia dwa biliony osiemset dwadzieścia cztery miliardy trzysta osiemdziesiąt cztery miliony dwieście dwadzieścia tysięcy dwieście dziewięćdzisiąt jeden")

def test_currency(self):
self.assertEqual(to_currency(1.0, 'EUR'), "jeden euro, zero centów")
self.assertEqual(to_currency(1.0, 'PLN'), "jeden złoty, zero groszy")
self.assertEqual(to_currency(1234.56, 'EUR'), "tysiąc dwieście trzydzieści cztery euro, pięćdziesiąt sześć centów")
self.assertEqual(to_currency(1234.56, 'PLN'), "tysiąc dwieście trzydzieści cztery złote, pięćdziesiąt sześć groszy")
self.assertEqual(to_currency(10111, 'EUR', seperator=' i'), "sto jeden euro i jedenaście centów")
self.assertEqual(to_currency(10121, 'PLN', seperator=' i'), "sto jeden złotych i dwadzieścia jeden groszy")
self.assertEqual(to_currency(-1251985, cents = False), "minus dwanaście tysięcy pięćset dziewiętnaście euro, 85 centów")
self.assertEqual(to_currency(123.50, 'PLN', seperator=' i'), "sto dwadzieścia trzy złote i pięćdziesiąt groszy")
self.assertEqual(to_currency(1950, cents = False), "dziewiętnaście euro, 50 centów")

0 comments on commit 710a3b6

Please sign in to comment.