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') ) )
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)
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), )
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
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")))
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")))
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)
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)
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}, )
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)
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})
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
def test_get_currency_precision(): assert get_currency_precision('EUR') == 2 assert get_currency_precision('JPY') == 0
def quantize_price(price: MoneyTypes, currency: str) -> MoneyTypes: precision = get_currency_precision(currency) number_places = Decimal(10)**-precision return price.quantize(number_places)
def money_to_integer(money): return int(money.amount * (10**get_currency_precision(money.currency.code)))
def precision(self): """Returns the precision of this currency.""" return get_currency_precision(self._code)
def test_get_currency_precision(): assert get_currency_precision('EUR') == 2 assert get_currency_precision('JPY') == 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