PyDocs

Nav

Numeric and Mathematical Modules

Decimal

decimal

class · Added in 3.6
  • Decimal(value='0', context=None)
Construct a new Decimal object.

value can be an integer, string, tuple, or another Decimal object.  If
no value is given, return Decimal('0'). The context does not affect
the conversion and is only passed to determine if the InvalidOperation
trap is active.
Changed in 3.2, 3.3, 3.6
  • 3.2: The argument to the constructor is now permitted to be a float instance.
  • 3.2: Mixed-type comparisons between Decimal instances and other numeric types are now fully supported.
  • 3.3: float arguments raise an exception if the FloatOperation trap is set. By default the trap is off.
  • 3.6: Underscores are allowed for grouping, as with integral and floating-point literals in code.
Members of Decimal

a

adjusted
Return the adjusted exponent (exp + digits - 1) of the number.
as_integer_ratio
Return a pair of integers whose ratio is exactly equal to the original.
as_tuple
Return a tuple representation of the number.

c

canonical
Return the canonical encoding of the argument.
compare
Compare self to other.
compare_signal
Identical to compare, except that all NaNs signal.
compare_total
Compare two operands using their abstract representation.
compare_total_mag
As compare_total(), but ignores the sign of each operand.
conjugate
Return self.
copy_abs
Return the absolute value of the argument.
copy_negate
Return the negation of the argument.
copy_sign
Return a copy of *self* with the sign of *other*.

e

exp
Return the value of the (natural) exponential function e**x.

f

fma
Fused multiply-add.
from_float
Alternative constructor that only accepts instances of float or int.
from_number
Alternative constructor that only accepts instances of float, int or Decimal, but not strings or tuples.

i

is_canonical
Return True if the argument is canonical and False otherwise.
is_finite
Return True if the argument is a finite number, and False otherwise.
is_infinite
Return True if the argument is infinite, and False otherwise.
is_nan
Return True if the argument is a (quiet or signaling) NaN, else False.
is_normal
Return True if the argument is a normal number and False otherwise.
is_qnan
Return True if the argument is a quiet NaN, and False otherwise.
is_signed
Return True if the argument has a negative sign and False otherwise.
is_snan
Return True if the argument is a signaling NaN and False otherwise.
is_subnormal
Return True if the argument is subnormal, and False otherwise.
is_zero
Return True if the argument is a zero and False otherwise.

l

ln
Return the natural (base e) logarithm of the operand.
log10
Return the base ten logarithm of the operand.
logb
Return the adjusted exponent of the operand as a Decimal instance.
logical_and
Applies an 'and' operation between self and other's digits.
logical_invert
Invert all its digits.
logical_or
Applies an 'or' operation between self and other's digits.
logical_xor
Applies an 'xor' operation between self and other's digits.

m

max
Maximum of self and other.
max_mag
As the max() method, but compares the absolute values of the operands.
min
Minimum of self and other.
min_mag
As the min() method, but compares the absolute values of the operands.

n

next_minus
Returns the largest representable number smaller than itself.
next_plus
Returns the smallest representable number larger than itself.
next_toward
Returns the number closest to self, in the direction towards other.
normalize
Normalize the number by stripping trailing 0s
number_class
Return a string describing the class of the operand.

q

quantize
Quantize *self* so its exponent is the same as that of *exp*.

r

radix
Return Decimal(10).
remainder_near
Return the remainder from dividing self by other.
rotate
Returns a rotated copy of self's digits, value-of-other times.

s

same_quantum
Test whether self and other have the same exponent or both are NaN.
scaleb
Return the first operand with the exponent adjusted the second.
shift
Returns a shifted copy of self's digits, value-of-other times.
sqrt
Return the square root of the argument to full precision.

t

to_eng_string
Convert to an engineering-type string.
to_integral
Identical to the to_integral_value() method.
to_integral_exact
Round to the nearest integer.
to_integral_value
Round to the nearest integer without signaling Inexact or Rounded.
1 Example
>>> (-7) % 4
1
>>> Decimal(-7) % Decimal(4)
Decimal('-3')
from the Python docs
See Also
No see-alsos for decimal/Decimal
0 Notes
No notes for Decimal