Skip to content
Snippets Groups Projects
Commit 87727cba authored by Cédric Krier's avatar Cédric Krier :atom:
Browse files

Ensure precision is great enough when rounding

The quantize method raise InvalidOperation if the length of the coefficient
after the quantize operation would be greater than precision.

issue6534
review255701002
(grafted from 5279aed1e54b)
parent 1fb663ed
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
import datetime
import json
from decimal import Decimal, ROUND_HALF_EVEN
from decimal import Decimal, ROUND_HALF_EVEN, localcontext
from trytond.model import ModelView, ModelSQL, fields, Unique, Check
from trytond.tools import datetime_strftime
from trytond.transaction import Transaction
......@@ -196,8 +196,12 @@
def round(self, amount, rounding=ROUND_HALF_EVEN):
'Round the amount depending of the currency'
return (amount / self.rounding).quantize(Decimal('1.'),
rounding=rounding) * self.rounding
with localcontext() as ctx:
ctx.prec = max(ctx.prec, (amount / self.rounding).adjusted() + 1)
# Divide and multiple by rounding for case rounding is not 10En
result = (amount / self.rounding).quantize(Decimal('1.'),
rounding=rounding) * self.rounding
return Decimal(result)
def is_zero(self, amount):
'Return True if the amount can be considered as zero for the currency'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment