python decimal
unknown
python
a year ago
1.3 kB
57
Indexable
Never
import decimal class BigDecimal(decimal.Decimal): """ Compatibility class for decimal.Decimal """ ROUND_DOWN = decimal.ROUND_DOWN ROUND_UP = decimal.ROUND_UP @classmethod def _mk_exp(cls, prec): return cls('0.' + '0' * prec) def divide(self, other, scale=None, rounding=None): if not scale and not rounding: return BigDecimal(self / other) if type(scale) is not int: raise ValueError("Expected integer value for scale") exp = BigDecimal._mk_exp(scale) return BigDecimal((self / other).quantize(exp, rounding=rounding)) @classmethod def valueOf(cls, value): return cls(value) def multiply(self, other): return BigDecimal(self * other) def setScale(self, scale, rounding): exp = BigDecimal._mk_exp(scale) return BigDecimal(self.quantize(exp, rounding=rounding)) def add(self, other): return BigDecimal(self + other) def subtract(self, other): return BigDecimal(self - other) def longValue(self): return int(self) def compareTo(self, other): return BigDecimal(self.compare(other)) BigDecimal.ZERO = BigDecimal(0) BigDecimal.ONE = BigDecimal(1) BigDecimal.TEN = BigDecimal(10)