Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parse_currency_parts(self):
# integer with cents
self.assertEqual(parse_currency_parts(101), (1, 1, False))
self.assertEqual(parse_currency_parts(-123), (1, 23, True))
# integer without cents
self.assertEqual(parse_currency_parts(101, is_int_with_cents=False),
(101, 0, False))
self.assertEqual(parse_currency_parts(-123, is_int_with_cents=False),
(123, 0, True))
# float
self.assertEqual(parse_currency_parts(1.01), (1, 1, False))
self.assertEqual(parse_currency_parts(-1.23), (1, 23, True))
self.assertEqual(parse_currency_parts(-1.2), (1, 20, True))
self.assertEqual(parse_currency_parts(0.004), (0, 0, False))
self.assertEqual(parse_currency_parts(0.005), (0, 1, False))
self.assertEqual(parse_currency_parts(0.006), (0, 1, False))
self.assertEqual(parse_currency_parts(0.0005), (0, 0, False))
self.assertEqual(parse_currency_parts(0.984), (0, 98, False))
self.assertEqual(parse_currency_parts(0.989), (0, 99, False))
self.assertEqual(parse_currency_parts(0.994), (0, 99, False))
self.assertEqual(parse_currency_parts(0.999), (1, 0, False))
# self.assertEqual(parse_currency_parts(0.985), (0, 99, False))
# self.assertEqual(parse_currency_parts(0.995), (1, 0, False))
# decimal
self.assertEqual(parse_currency_parts(Decimal("1.01")), (1, 1, False))
self.assertEqual(parse_currency_parts(Decimal("-1.23")), (1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.233")),
(1, 23, True))
# integer without cents
self.assertEqual(parse_currency_parts(101, is_int_with_cents=False),
(101, 0, False))
self.assertEqual(parse_currency_parts(-123, is_int_with_cents=False),
(123, 0, True))
# float
self.assertEqual(parse_currency_parts(1.01), (1, 1, False))
self.assertEqual(parse_currency_parts(-1.23), (1, 23, True))
self.assertEqual(parse_currency_parts(-1.2), (1, 20, True))
self.assertEqual(parse_currency_parts(0.004), (0, 0, False))
self.assertEqual(parse_currency_parts(0.005), (0, 1, False))
self.assertEqual(parse_currency_parts(0.006), (0, 1, False))
self.assertEqual(parse_currency_parts(0.0005), (0, 0, False))
self.assertEqual(parse_currency_parts(0.984), (0, 98, False))
self.assertEqual(parse_currency_parts(0.989), (0, 99, False))
self.assertEqual(parse_currency_parts(0.994), (0, 99, False))
self.assertEqual(parse_currency_parts(0.999), (1, 0, False))
# self.assertEqual(parse_currency_parts(0.985), (0, 99, False))
# self.assertEqual(parse_currency_parts(0.995), (1, 0, False))
# decimal
self.assertEqual(parse_currency_parts(Decimal("1.01")), (1, 1, False))
self.assertEqual(parse_currency_parts(Decimal("-1.23")), (1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.233")),
(1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.989")),
(1, 99, True))
# string
self.assertEqual(parse_currency_parts("1.01"), (1, 1, False))
self.assertEqual(parse_currency_parts("-1.23"), (1, 23, True))
self.assertEqual(parse_currency_parts(0.999), (1, 0, False))
# self.assertEqual(parse_currency_parts(0.985), (0, 99, False))
# self.assertEqual(parse_currency_parts(0.995), (1, 0, False))
# decimal
self.assertEqual(parse_currency_parts(Decimal("1.01")), (1, 1, False))
self.assertEqual(parse_currency_parts(Decimal("-1.23")), (1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.233")),
(1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.989")),
(1, 99, True))
# string
self.assertEqual(parse_currency_parts("1.01"), (1, 1, False))
self.assertEqual(parse_currency_parts("-1.23"), (1, 23, True))
self.assertEqual(parse_currency_parts("-1.2"), (1, 20, True))
self.assertEqual(parse_currency_parts("1"), (1, 0, False))
self.assertEqual(parse_currency_parts(0.994), (0, 99, False))
self.assertEqual(parse_currency_parts(0.999), (1, 0, False))
# self.assertEqual(parse_currency_parts(0.985), (0, 99, False))
# self.assertEqual(parse_currency_parts(0.995), (1, 0, False))
# decimal
self.assertEqual(parse_currency_parts(Decimal("1.01")), (1, 1, False))
self.assertEqual(parse_currency_parts(Decimal("-1.23")), (1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.233")),
(1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.989")),
(1, 99, True))
# string
self.assertEqual(parse_currency_parts("1.01"), (1, 1, False))
self.assertEqual(parse_currency_parts("-1.23"), (1, 23, True))
self.assertEqual(parse_currency_parts("-1.2"), (1, 20, True))
self.assertEqual(parse_currency_parts("1"), (1, 0, False))
self.assertEqual(parse_currency_parts(0.989), (0, 99, False))
self.assertEqual(parse_currency_parts(0.994), (0, 99, False))
self.assertEqual(parse_currency_parts(0.999), (1, 0, False))
# self.assertEqual(parse_currency_parts(0.985), (0, 99, False))
# self.assertEqual(parse_currency_parts(0.995), (1, 0, False))
# decimal
self.assertEqual(parse_currency_parts(Decimal("1.01")), (1, 1, False))
self.assertEqual(parse_currency_parts(Decimal("-1.23")), (1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.233")),
(1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.989")),
(1, 99, True))
# string
self.assertEqual(parse_currency_parts("1.01"), (1, 1, False))
self.assertEqual(parse_currency_parts("-1.23"), (1, 23, True))
self.assertEqual(parse_currency_parts("-1.2"), (1, 20, True))
self.assertEqual(parse_currency_parts("1"), (1, 0, False))
# self.assertEqual(parse_currency_parts(0.985), (0, 99, False))
# self.assertEqual(parse_currency_parts(0.995), (1, 0, False))
# decimal
self.assertEqual(parse_currency_parts(Decimal("1.01")), (1, 1, False))
self.assertEqual(parse_currency_parts(Decimal("-1.23")), (1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.233")),
(1, 23, True))
self.assertEqual(parse_currency_parts(Decimal("-1.989")),
(1, 99, True))
# string
self.assertEqual(parse_currency_parts("1.01"), (1, 1, False))
self.assertEqual(parse_currency_parts("-1.23"), (1, 23, True))
self.assertEqual(parse_currency_parts("-1.2"), (1, 20, True))
self.assertEqual(parse_currency_parts("1"), (1, 0, False))
def __init__(self, number, lang='es', currency='PEN', separator='con', format=DEFAULT_FORMATS[0]):
self.number = number
self.lang = lang
self.lang_object = self.get_lang_object(lang)
self.currency = currency
self.separator = separator.strip()
self.format = format
self.integer_part, self.decimal_part, self.negative = parse_currency_parts(number)
self.custom_currency_form = None
def to_currency(self, val, currency="JPY", cents=False, separator="",
adjective=False, reading=False, prefer=None):
left, right, is_negative = parse_currency_parts(
val, is_int_with_cents=cents)
try:
cr1, cr2 = self.CURRENCY_FORMS[currency]
if (cents or abs(val) != left) and not cr2:
raise ValueError('Decimals not supported for "%s"' % 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 = self.negword if is_negative else ""
def round_2_decimal(self, number):
integer, cents, negative = parse_currency_parts(
number, is_int_with_cents=False
)
integer = '{}'.format(integer)
cents = '{}'.format(cents)
if len(cents) < 2:
add_zero = 2 - len(cents)
cents = ('0' * add_zero) + cents
text_num = integer + '.' + cents
return text_num, negative