我想知道是否必须将discount_value
转换成十进制(以确保)。似乎当我用print
折扣值测试它时,它的类型被自动检测为十进制。然而,我以前有过这样的情况,它被检测为一个浮动,不再工作了。你知道吗
def calculate_discounted_value(self, ticket_price_gross):
[...]
elif self.percentage:
discount_value = Decimal(ticket_price_gross * self.percentage)
discount_value = quantize(discount_value, '1')
print(ticket_price_gross - discount_value, "PERCENTAGE")
def quantize(amount, decimals):
"""
Decimal numbers can be represented exactly. In contrast, numbers like 1.1 and
2.2 do not have exact representations in binary floating point. End users
typically would not expect 1.1 + 2.2 to display as 3.3000000000000003 as it
does with binary floating point. With this function we get better control about
rounding.
Therefore: amount should be come in as decimal.
"""
#amount_as_decimal = Decimal(amount)
amount_as_decimal = amount
quantized_amount = amount_as_decimal.quantize(
Decimal(decimals),
rounding=ROUND_HALF_UP
)
return quantized_amount
似乎没有必要在将
discount_value
传递给quantize()
之前生成Decimal()
,因为quantize
转换为Decimal()
。出于同样的原因,注释行似乎没有必要,但整个quantize()
函数也是如此。为什么不这样做呢?你知道吗另外请注意this关于
quantize()
:相关问题 更多 >
编程相关推荐