Beispiel #1
0
    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
Beispiel #2
0
    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
Beispiel #3
0
 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
Beispiel #4
0
def actualizar_leyendas(monto, moneda, separador):
    MONEDAS = {
        'PEN': 'soles',
        'USD': 'dolares americanos',
    }

    leyenda = ''
    try:
        left, rigth, is_negative = parse_currency_parts(
            monto, is_int_with_cents=False)

        if left > 0:
            leyenda = '{} {}'.format(num2words(left, lang='es'), separador)
        cent = '{}/100 {}'.format(rigth, MONEDAS[moneda])
        leyenda = '{} {}'.format(leyenda, cent)
        return leyenda.upper()

    except NotImplementedError:
        return ''
Beispiel #5
0
    def test_parse_currency_parts(self):
        # integer cents
        self.assertEqual(parse_currency_parts(101), (1, 1, False))
        self.assertEqual(parse_currency_parts(-123), (1, 23, True))

        # 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))

        # 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))

        # 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))
Beispiel #6
0
    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))
        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))
Beispiel #7
0
    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))
        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))