Beispiel #1
0
 def normalize_fraction(self, value):
     decimal = BaseDecimal(value, self.context)
     normalized = decimal.normalize()
     sign, digits, exponent = normalized.as_tuple()
     if exponent > 0:
         return BaseDecimal((sign, digits + (0, ) * exponent, 0),
                            self.context)
     else:
         return normalized
Beispiel #2
0
 def format(self, value):
     dvalue = BaseDecimal(value)
     if not dvalue.is_normal() and dvalue != ZERO:
         raise MarshallingException('Invalid Fixed precision number.')
     return str(dvalue.quantize(self.precision, rounding=ROUND_HALF_EVEN))
Beispiel #3
0
 def __init__(self, decimals=5, **kwargs):
     super(Fixed, self).__init__(**kwargs)
     self.precision = BaseDecimal('0.' + '0' * (decimals - 1) + '1')
Beispiel #4
0
 def format(self, value):
     return str(BaseDecimal(value))
Beispiel #5
0
    def format(self, value):
        try:
            if self.dt_format == 'rfc822':
                return _rfc822(value)
            elif self.dt_format == 'iso8601':
                return _iso8601(value)
            else:
                raise MarshallingException(
                    'Unsupported date format %s' % self.dt_format
                )
        except AttributeError as ae:
            raise MarshallingException(ae)


ZERO = BaseDecimal()


class Fixed(Raw):
    """
    A decimal number with a fixed precision.
    """
    def __init__(self, decimals=5, **kwargs):
        super(Fixed, self).__init__(**kwargs)
        self.precision = BaseDecimal('0.' + '0' * (decimals - 1) + '1')

    def format(self, value):
        dvalue = BaseDecimal(value)
        if not dvalue.is_normal() and dvalue != ZERO:
            raise MarshallingException('Invalid Fixed precision number.')
        return str(dvalue.quantize(self.precision, rounding=ROUND_HALF_EVEN))
Beispiel #6
0
 def __call__(self, value):
     return BaseDecimal(value, self.context)
Beispiel #7
0
 def __call__(self, value):
     value = super().__call__(value)
     value = BaseDecimal(value)
     return value