Esempio n. 1
0
    def __init__(self, value, precision=None, scale=None):
        """ create an decimal object, precision and scale are 0 by default
        and precision is limited under 1000
        """
        if (precision is None and scale is not None) or (precision is not None
                                                         and scale is None):
            raise TypeError(
                "precision and scale should be set both or neither")
        if precision is not None and not isinstance(precision, int):
            raise TypeError("precision must be an instance of int")
        if scale is not None and not isinstance(scale, int):
            raise TypeError("scale must be an instance of int")

        _, self.__decimal = decimal.create()
        if _ != 0:
            raise InvalidDecimal("cannot create decimal entity, out of memory")

        if precision is None and scale is None:
            _ = decimal.init(self.__decimal)
        else:
            _ = decimal.init2(self.__decimal, precision, scale)
        if 0 != _:
            raise InvalidDecimal("invalid parameter, code: %d" % _)

        self.__parse(value)
Esempio n. 2
0
 def _from_bson_element_value(self, value):
     """the method is used by bson.BSON.decode to decode the binary string(an bson element value) into an decimal value
     """
     _, l = decimal.fromBsonValue(self.__decimal, value)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
     return l
Esempio n. 3
0
 def _to_bson_element_value(self):
     """the method is used to encode an decimal object into the value of bson element
     """
     _, s = decimal.toBsonElement(self.__decimal)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
     return s
Esempio n. 4
0
 def to_float(self):
     """force the decimal to be an float(double is supported), and show it regularized by scale
     """
     _, v = decimal.toFloat(self.__decimal)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
     return v
Esempio n. 5
0
 def to_string(self):
     """force the decimal to be an string, and show it regularized by scale
     """
     _, v = decimal.toString(self.__decimal)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
     return v
Esempio n. 6
0
 def is_max(self):
     """charge the value of decimal is min value or not
     """
     _, max_ = decimal.isMax(self.__decimal)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
     return True if max_ != 0 else False
Esempio n. 7
0
 def is_zero(self):
     """charge the value of decimal is zero ir not
     """
     _, zero_ = decimal.isZero(self.__decimal)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
     return True if zero_ != 0 else False
Esempio n. 8
0
    def compare(self, rhs):
        """compare between two decimal object.
        if int is specified, int value will be converted to an decimal object, then compare
        """
        if isinstance(rhs, long_type):
            _ = self.compare(Decimal(rhs))
        elif isinstance(rhs, int):
            _ = decimal.compareInt(self.__decimal, rhs)
        elif isinstance(rhs, float):
            _ = self.compare(Decimal(rhs))
        elif isinstance(rhs, Decimal):
            _ = decimal.compare(self.__decimal, rhs.__decimal)
        else:
            raise TypeError('invalid comparision between Decimal and %s' %
                            type(rhs))

        if _ not in (-1, 0, 1):
            raise InvalidDecimal(
                "invalid return value or process failed, code %d" % _)
        return _
Esempio n. 9
0
 def __to_json_string(self):
     _, v = decimal.toJsonString(self.__decimal)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
     return v
Esempio n. 10
0
 def __from_string(self, value):
     _ = decimal.fromString(self.__decimal, value)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
Esempio n. 11
0
 def set_max(self):
     """set the value of decimal is the max value
     """
     _ = decimal.setMax(self.__decimal)
     if _ != 0:
         raise InvalidDecimal("invalid parameter, code: %d" % _)
Esempio n. 12
0
 def set_zero(self):
     """set the decimal object as an instance initalized by 0
     """
     _ = decimal.setZero(self.__decimal)
     if 0 != _:
         raise InvalidDecimal("invalid parameter, code: %d" % _)