Пример #1
0
def money_to_integer(money):
    try:
        return int(
            money.amount * (
                10 ** get_currency_precision(money.currency.code)
            )
        )
    except AttributeError as e:
        return int(
            int(money) * (
                10 ** get_currency_precision('usd')
            )
        )
Пример #2
0
def quantize_price(
        price: Union["TaxedMoney", "Money", Decimal,
                     "TaxedMoneyRange"], currency: str
) -> Union["TaxedMoney", "Money", Decimal, "TaxedMoneyRange"]:
    precision = get_currency_precision(currency)
    number_places = Decimal(10)**-precision
    return price.quantize(number_places)
Пример #3
0
def make_standard_tip(label, weekly, currency):
    precision = get_currency_precision(currency)
    minimum = D_CENT if precision == 2 else Decimal(10) ** (-precision)
    return StandardTip(
        label,
        Money(weekly, currency),
        Money((weekly / PERIOD_CONVERSION_RATES['monthly']).quantize(minimum), currency),
        Money((weekly / PERIOD_CONVERSION_RATES['yearly']).quantize(minimum), currency),
    )
Пример #4
0
def price_from_minor_unit(value: str, currency: str):
    """Convert minor unit (smallest unit of currency) to decimal value.

    (value: 1000, currency: USD) will be converted to 10.00
    """

    value = Decimal(value)
    precision = get_currency_precision(currency)
    number_places = Decimal(10) ** -precision
    return value * number_places
Пример #5
0
def to_adyen_price(value: Decimal, currency: str):
    """Adyen doesn't use values with comma.

    Take the value, discover the precision of currency and multiply value by
    Decimal('10.0'), then change quantization to remove the comma.
    """
    value = quantize_price(value, currency=currency)
    precision = get_currency_precision(currency)
    number_places = Decimal("10.0") ** precision
    value_without_comma = value * number_places
    return str(value_without_comma.quantize(Decimal("1")))
Пример #6
0
def price_to_minor_unit(value: Decimal, currency: str):
    """Convert decimal value to the smallest unit of currency.

    Take the value, discover the precision of currency and multiply value by
    Decimal('10.0'), then change quantization to remove the comma.
    Decimal(10.0) -> str(1000)
    """
    value = quantize_price(value, currency=currency)
    precision = get_currency_precision(currency)
    number_places = Decimal("10.0") ** precision
    value_without_comma = value * number_places
    return str(value_without_comma.quantize(Decimal("1")))
Пример #7
0
    def quantize(self, exp=None, rounding=None) -> 'Money':
        """Return a copy of the object with its amount quantized.

        If `exp` is given the resulting exponent will match that of `exp`.

        Otherwise the resulting exponent will be set to the correct exponent
        of the currency if it's known and to default (two decimal places)
        otherwise.
        """
        if exp is None:
            digits = get_currency_precision(self.currency)
            exp = Decimal('0.1')**digits
        else:
            exp = Decimal(exp)
        return Money(self.amount.quantize(exp, rounding=rounding),
                     self.currency)
Пример #8
0
    def quantize(self, exp=None, rounding=None) -> 'Money':
        """Return a copy of the object with its amount quantized.

        If `exp` is given the resulting exponent will match that of `exp`.

        Otherwise the resulting exponent will be set to the correct exponent
        of the currency if it's known and to default (two decimal places)
        otherwise.
        """
        if rounding is None:
            rounding = ROUND_HALF_UP
        if exp is None:
            digits = get_currency_precision(self.currency)
            exp = Decimal('0.1') ** digits
        else:
            exp = Decimal(exp)
        return Money(
            self.amount.quantize(exp, rounding=rounding), self.currency)
Пример #9
0
    def __call__(self, other):
        value = other.amount
        currency = other.currency
        super(MoneyPrecisionValidator, self).__call__(value)

        if is_currency(currency):
            currency_precision = get_currency_precision(currency)
            exponent = value.as_tuple()[-1]
            if exponent >= 0:
                decimals = 0
            else:
                decimals = abs(exponent)
            if decimals > currency_precision:
                raise ValidationError(
                    self.messages["max_decimal_places"],
                    code="max_decimal_places",
                    params={"max": currency_precision},
                )
Пример #10
0
 def quantize(self, exp=None, rounding=None) -> "Currency":
     """
     :param exp: fractional part of currency. Like cents in dollar. It can be set to any arbitrary amount.
     :param rounding: ROUND_DOWN, ROUND_UP, ROUND_HALF_UP
     :return: Currency object
     The quantize() method rounds a number to a fixed exponent.
     This method is useful for monetary applications that
     often round results to a fixed number of places
     Example: (10.001, 'USD').quantize(exp='.01', rounding=ROUND_DOWN)
     results in (10.00, 'USD')
     """
     if exp is None:
         exp = Decimal("0.1")**get_currency_precision(self.currency)
     else:
         exp = Decimal(exp)
     if rounding is None:
         rounding = ROUND_HALF_UP
     return Currency(self.amount.quantize(exp=exp, rounding=rounding),
                     self.currency)
Пример #11
0
    def __call__(self, other):
        if self.currency != other.currency:
            raise ValueError('Invalid currency: %r (expected %r)' % (
                other.currency, self.currency))

        value = other.amount
        super(MoneyPrecisionValidator, self).__call__(value)

        if is_currency(self.currency):
            currency_precision = get_currency_precision(self.currency)
            exponent = value.as_tuple()[-1]
            if exponent >= 0:
                decimals = 0
            else:
                decimals = abs(exponent)
            if decimals > currency_precision:
                raise ValidationError(
                    self.messages['max_decimal_places'],
                    code='max_decimal_places',
                    params={'max': currency_precision})
Пример #12
0
 def __missing__(self, currency):
     exponent = get_currency_precision(currency)
     minimum = Money(
         (D_CENT if exponent == 2 else Decimal(10)**(-exponent)), currency)
     self[currency] = minimum
     return minimum
Пример #13
0
def test_get_currency_precision():
    assert get_currency_precision('EUR') == 2
    assert get_currency_precision('JPY') == 0
Пример #14
0
def quantize_price(price: MoneyTypes, currency: str) -> MoneyTypes:
    precision = get_currency_precision(currency)
    number_places = Decimal(10)**-precision
    return price.quantize(number_places)
Пример #15
0
def money_to_integer(money):
    return int(money.amount *
               (10**get_currency_precision(money.currency.code)))
Пример #16
0
    def precision(self):
        """Returns the precision of this currency."""

        return get_currency_precision(self._code)
Пример #17
0
def test_get_currency_precision():
    assert get_currency_precision('EUR') == 2
    assert get_currency_precision('JPY') == 0
Пример #18
0
def from_adyen_price(value: str, currency: str):
    value = Decimal(value)
    precision = get_currency_precision(currency)
    number_places = Decimal(10) ** -precision
    return value * number_places