dec_round¶
-
privex.helpers.common.
dec_round
(amount: decimal.Decimal, dp: int = 2, rounding=None) → decimal.Decimal[source]¶ Round a Decimal to x decimal places using
quantize
(dp
must be >= 1 and the default dp is 2)If you don’t specify a rounding option, it will use whatever rounding has been set in
decimal.getcontext()
(most python versions have this default toROUND_HALF_EVEN
)Basic Usage:
>>> from decimal import Decimal, getcontext, ROUND_FLOOR >>> x = Decimal('1.9998') >>> dec_round(x, 3) Decimal('2.000')
Custom Rounding as an argument:
>>> dec_round(x, 3, rounding=ROUND_FLOOR) Decimal('1.999')
Override context rounding to set the default:
>>> getcontext().rounding = ROUND_FLOOR >>> dec_round(x, 3) Decimal('1.999')