mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 06:42:25 +00:00
Refactor and fix some issues with to_currency. Reduce code duplication. (#129)
* Refactor and fix some issues with to_currency. Reduce code duplication. * Use unicode literals in uk testcase.
This commit is contained in:
committed by
Ernesto Rodriguez Ortiz
parent
abae0b56a2
commit
f9d8868794
37
num2words/currency.py
Normal file
37
num2words/currency.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from __future__ import division
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
def parse_currency_parts(value):
|
||||
if isinstance(value, int):
|
||||
# assume cents if value is integer
|
||||
negative = value < 0
|
||||
value = abs(value)
|
||||
integer, cents = divmod(value, 100)
|
||||
|
||||
elif isinstance(value, Decimal):
|
||||
negative = value < 0
|
||||
value = abs(value)
|
||||
integer, fraction = divmod(value, 1)
|
||||
integer = int(integer)
|
||||
cents = int(fraction * 100)
|
||||
|
||||
else:
|
||||
# @TODO consider using something (babel) that does locale aware parsing
|
||||
value = str(value).replace(',', '.')
|
||||
negative = value.startswith('-')
|
||||
|
||||
if negative:
|
||||
value = value.lstrip('-')
|
||||
|
||||
if '.' in value:
|
||||
integer, fraction = value.rsplit('.', 1)
|
||||
fraction = fraction.ljust(2, "0")
|
||||
else:
|
||||
integer, fraction = value, 0
|
||||
|
||||
integer = int(integer)
|
||||
cents = int(fraction)
|
||||
|
||||
return integer, cents, negative
|
||||
@@ -95,6 +95,8 @@ vienas tūkstantis du šimtai trisdešimt keturi eurai, penkiasdešimt šeši ce
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .currency import parse_currency_parts
|
||||
|
||||
ZERO = (u'nulis',)
|
||||
|
||||
ONES = {
|
||||
@@ -223,34 +225,11 @@ def n2w(n):
|
||||
|
||||
|
||||
def to_currency(n, currency='EUR', cents=True):
|
||||
if type(n) == int:
|
||||
if n < 0:
|
||||
minus = True
|
||||
else:
|
||||
minus = False
|
||||
|
||||
n = abs(n)
|
||||
left = n / 100
|
||||
right = n % 100
|
||||
else:
|
||||
n = str(n).replace(',', '.')
|
||||
if '.' in n:
|
||||
left, right = n.split('.')
|
||||
else:
|
||||
left, right = n, 0
|
||||
left, right = int(left), int(right)
|
||||
minus = False
|
||||
left, right, is_negative = parse_currency_parts(n)
|
||||
cr1, cr2 = CURRENCIES[currency]
|
||||
|
||||
if minus:
|
||||
minus_str = "minus "
|
||||
else:
|
||||
minus_str = ""
|
||||
|
||||
if cents:
|
||||
cents_str = int2word(right)
|
||||
else:
|
||||
cents_str = "%02d" % right
|
||||
minus_str = "minus " if is_negative else ""
|
||||
cents_str = int2word(right) if cents else "%02d" % right
|
||||
|
||||
return u'%s%s %s, %s %s' % (minus_str, int2word(left),
|
||||
pluralize(left, cr1),
|
||||
|
||||
@@ -99,6 +99,8 @@ mīnus divpadsmit tūkstoši pieci simti deviņpadsmit eiro, 85 centi
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .currency import parse_currency_parts
|
||||
|
||||
ZERO = (u'nulle',)
|
||||
|
||||
ONES = {
|
||||
@@ -233,34 +235,11 @@ def n2w(n):
|
||||
|
||||
|
||||
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
|
||||
right = n % 100
|
||||
else:
|
||||
n = str(n).replace(',', '.')
|
||||
if '.' in n:
|
||||
left, right = n.split('.')
|
||||
else:
|
||||
left, right = n, 0
|
||||
left, right = int(left), int(right)
|
||||
minus = False
|
||||
left, right, is_negative = parse_currency_parts(n)
|
||||
cr1, cr2 = CURRENCIES[currency]
|
||||
|
||||
if minus:
|
||||
minus_str = "mīnus "
|
||||
else:
|
||||
minus_str = ""
|
||||
|
||||
if cents:
|
||||
cents_str = int2word(right)
|
||||
else:
|
||||
cents_str = "%02d" % right
|
||||
minus_str = "mīnus " if is_negative else ""
|
||||
cents_str = int2word(right) if cents else "%02d" % right
|
||||
|
||||
return u'%s%s %s%s %s %s' % (
|
||||
minus_str,
|
||||
|
||||
@@ -104,6 +104,8 @@ sto dwadzieścia trzy złote i pięćdziesiąt groszy
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .currency import parse_currency_parts
|
||||
|
||||
ZERO = (u'zero',)
|
||||
|
||||
ONES = {
|
||||
@@ -241,36 +243,11 @@ def n2w(n):
|
||||
|
||||
|
||||
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
|
||||
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)
|
||||
minus = False
|
||||
left, right, is_negative = parse_currency_parts(n)
|
||||
cr1, cr2 = CURRENCIES[currency]
|
||||
|
||||
if minus:
|
||||
minus_str = "minus "
|
||||
else:
|
||||
minus_str = ""
|
||||
|
||||
if cents:
|
||||
cents_str = int2word(right)
|
||||
else:
|
||||
cents_str = "%02d" % right
|
||||
minus_str = "minus " if is_negative else ""
|
||||
cents_str = int2word(right) if cents else "%02d" % right
|
||||
|
||||
return u'%s%s %s%s %s %s' % (
|
||||
minus_str,
|
||||
|
||||
@@ -103,6 +103,8 @@ u"""
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .currency import parse_currency_parts
|
||||
|
||||
ZERO = (u'ноль',)
|
||||
|
||||
ONES_FEMININE = {
|
||||
@@ -259,29 +261,10 @@ def n2w(n):
|
||||
|
||||
|
||||
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
|
||||
right = n % 100
|
||||
else:
|
||||
n = str(n).replace(',', '.')
|
||||
if '.' in n:
|
||||
left, right = n.split('.')
|
||||
else:
|
||||
left, right = n, 0
|
||||
left, right = int(left), int(right)
|
||||
minus = False
|
||||
left, right, is_negative = parse_currency_parts(n)
|
||||
cr1, cr2 = CURRENCIES[currency]
|
||||
|
||||
if minus:
|
||||
minus_str = "минус "
|
||||
else:
|
||||
minus_str = ""
|
||||
minus_str = "минус " if is_negative else ""
|
||||
|
||||
if cents:
|
||||
cents_feminine = currency == 'RUB'
|
||||
|
||||
@@ -103,6 +103,8 @@ u"""
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .currency import parse_currency_parts
|
||||
|
||||
ZERO = (u'нуль',)
|
||||
|
||||
ONES_FEMININE = {
|
||||
@@ -264,29 +266,10 @@ def n2w(n):
|
||||
|
||||
|
||||
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
|
||||
right = n % 100
|
||||
else:
|
||||
n = str(n).replace(',', '.')
|
||||
if '.' in n:
|
||||
left, right = n.split('.')
|
||||
else:
|
||||
left, right = n, 0
|
||||
left, right = int(left), int(right)
|
||||
minus = False
|
||||
left, right, is_negative = parse_currency_parts(n)
|
||||
cr1, cr2 = CURRENCIES[currency]
|
||||
|
||||
if minus:
|
||||
minus_str = "мiнус "
|
||||
else:
|
||||
minus_str = ""
|
||||
minus_str = "мiнус " if is_negative else ""
|
||||
|
||||
if cents:
|
||||
cents_feminine = currency == 'UAH'
|
||||
|
||||
Reference in New Issue
Block a user