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 to ROUND_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')
Parameters
  • amount (Decimal) – The amount (as a Decimal) to round

  • dp (int) – Number of decimal places to round amount to. (Default: 2)

  • rounding (str) – A decimal rounding option, e.g. ROUND_HALF_EVEN or ROUND_FLOOR

Return Decimal rounded

The rounded Decimal amount