在 Odoo10 中使用舍入函数仍然将价格 34.50 舍入为 34
Using round function still price 34.50 round to 34 in Odoo10
我使用的是浮动价格的整数值,但我的价格 34.50 仍然被四舍五入为 34。达到 34.50 时应该是 35。主要是它的工作正常,但我不明白为什么它不能在 3000 的价格百分比折扣是 1.15
我也附上屏幕和代码。
@api.one
@api.depends('discount_type','discount_rate','amount_total')
def _compute_discount(self):
mod_obj = self.env['ir.model.data']
amount_discount = 0.0
if self.discount_type == 'percent':
amount_discount = self.amount_untaxed * self.discount_rate / 100
else:
amount_discount = self.discount_rate
self.amount_discount = round(amount_discount)
提前致谢
你得到这个结果是因为 python 1.15*3000 = 34.4999999 和 34.4999 的回合是 34
我找到了这个问题的解决方案。这是由于 python 和大多数编程语言中的浮点数问题。
将 3000 * 1.15 / 100.0 输入 python 终端实际上会得到 34.49999999999999,然后四舍五入为 34。
因为它们是价格,您可以使用 round(round(amount_discount, 2)) 将其四舍五入为 34.50,然后为 35。一般来说,这意味着任何大于 .495 的都被四舍五入。
我使用的是浮动价格的整数值,但我的价格 34.50 仍然被四舍五入为 34。达到 34.50 时应该是 35。主要是它的工作正常,但我不明白为什么它不能在 3000 的价格百分比折扣是 1.15
我也附上屏幕和代码。
@api.one
@api.depends('discount_type','discount_rate','amount_total')
def _compute_discount(self):
mod_obj = self.env['ir.model.data']
amount_discount = 0.0
if self.discount_type == 'percent':
amount_discount = self.amount_untaxed * self.discount_rate / 100
else:
amount_discount = self.discount_rate
self.amount_discount = round(amount_discount)
提前致谢
你得到这个结果是因为 python 1.15*3000 = 34.4999999 和 34.4999 的回合是 34
我找到了这个问题的解决方案。这是由于 python 和大多数编程语言中的浮点数问题。
将 3000 * 1.15 / 100.0 输入 python 终端实际上会得到 34.49999999999999,然后四舍五入为 34。
因为它们是价格,您可以使用 round(round(amount_discount, 2)) 将其四舍五入为 34.50,然后为 35。一般来说,这意味着任何大于 .495 的都被四舍五入。