numerics.py 556 B

1234567891011121314151617181920212223
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from decimal import Decimal, ROUND_HALF_UP
  4. def force_decimal(amount):
  5. """Given an amount of unknown type, type cast it to be a Decimal."""
  6. if not isinstance(amount, Decimal):
  7. return Decimal(str(amount))
  8. return amount
  9. def quantize(decimal_value, places = '0.01'):
  10. # type:(Decimal, str)->Decimal
  11. return decimal_value.quantize(Decimal(places), rounding = ROUND_HALF_UP)
  12. class UnitBase(object):
  13. @classmethod
  14. def initial(cls, default = '0'):
  15. return cls(default)