From: Raphael Michel Date: Fri, 7 Dec 2018 15:45:08 +0000 (+0100) Subject: Store amounts as Decimals, not floats, to prevent problems with the maximum length X-Git-Tag: v2.0.0~1^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d29ded896ba3cb9e9c72bf4455c952a2620eafad;p=thirdparty%2Fpython-fints.git Store amounts as Decimals, not floats, to prevent problems with the maximum length --- diff --git a/fints/fields.py b/fints/fields.py index 20600ff..47d6139 100644 --- a/fints/fields.py +++ b/fints/fields.py @@ -136,11 +136,31 @@ class FloatField(DataElementField): return retval -class AmountField(FixedLengthMixin, FloatField): +class AmountField(FixedLengthMixin, DataElementField): type = 'wrt' + _DOC_TYPE = decimal.Decimal _FIXED_LENGTH = [None, None, 15] # FIXME Needs test + def _parse_value(self, value): + if isinstance(value, float): + return value + + if isinstance(value, decimal.Decimal): + return value + + _value = str(value) + if not re.match(r'^(?:0|[1-9]\d*),(?:\d*[1-9]|)$', _value): + raise TypeError("Only digits and ',' allowed for value of type 'float', no superfluous leading or trailing zeroes allowed: {!r}".format(value)) + + return decimal.Decimal(_value.replace(",", ".")) + + def _render_value(self, value): + retval = str(value) + retval = retval.replace('.', ',').rstrip('0') + self._check_value_length(retval) + return retval + class BinaryField(DataElementField): type = 'bin'