mirror of
https://github.com/bblaz/num2words.git
synced 2025-12-06 14:52:25 +00:00
Attempt at refactoring to_currency into base (#135)
* Attempt at refactoring lv to base * Remove dead files. * Use static props. * Convert doctests, move utils out. * Refactor lt * Update and move ru doctests. * Refactor ru. * Refactor pl. * Move uk doctests. * Refactor uk. * DRY * Cleanup.
This commit is contained in:
committed by
Ernesto Rodriguez Ortiz
parent
1c699d1bb4
commit
1e954c909b
@@ -21,9 +21,13 @@ from collections import OrderedDict
|
||||
from decimal import Decimal
|
||||
|
||||
from .compat import to_s
|
||||
from .currency import parse_currency_parts, prefix_currency
|
||||
|
||||
|
||||
class Num2Word_Base(object):
|
||||
CURRENCY_FORMS = {}
|
||||
CURRENCY_ADJECTIVES = {}
|
||||
|
||||
def __init__(self):
|
||||
self.cards = OrderedDict()
|
||||
self.is_title = False
|
||||
@@ -243,8 +247,54 @@ class Num2Word_Base(object):
|
||||
def to_year(self, value, **kwargs):
|
||||
return self.to_cardinal(value)
|
||||
|
||||
def to_currency(self, value, **kwargs):
|
||||
return self.to_cardinal(value)
|
||||
def pluralize(self, n, forms):
|
||||
"""
|
||||
Should resolve gettext form:
|
||||
http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def _cents_verbose(self, number, currency):
|
||||
return self.to_cardinal(number)
|
||||
|
||||
def to_currency(self, val, currency='EUR', cents=True, seperator=',',
|
||||
adjective=False):
|
||||
"""
|
||||
Args:
|
||||
val: Numeric value
|
||||
currency (str): Currency code
|
||||
cents (bool): Verbose cents
|
||||
seperator (str): Cent seperator
|
||||
adjective (bool): Prefix currency name with adjective
|
||||
Returns:
|
||||
str: Formatted string
|
||||
|
||||
"""
|
||||
left, right, is_negative = parse_currency_parts(val)
|
||||
|
||||
try:
|
||||
cr1, cr2 = self.CURRENCY_FORMS[currency]
|
||||
|
||||
except KeyError:
|
||||
raise NotImplementedError(
|
||||
'Currency code "%s" not implemented for "%s"' %
|
||||
(currency, self.__class__.__name__))
|
||||
|
||||
if adjective and currency in self.CURRENCY_ADJECTIVES:
|
||||
cr1 = prefix_currency(self.CURRENCY_ADJECTIVES[currency], cr1)
|
||||
|
||||
minus_str = "%s " % self.negword if is_negative else ""
|
||||
cents_str = self._cents_verbose(right, currency) \
|
||||
if cents else "%02d" % right
|
||||
|
||||
return u'%s%s %s%s %s %s' % (
|
||||
minus_str,
|
||||
self.to_cardinal(left),
|
||||
self.pluralize(left, cr1),
|
||||
seperator,
|
||||
cents_str,
|
||||
self.pluralize(right, cr2)
|
||||
)
|
||||
|
||||
def base_setup(self):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user