terra_sdk.core.numeric
Numeric types.
DEC_NUM_DIGITS
Number of digits for Decimal.
chop_precision_and_round
_1def chop_precision_and_round(d: int) -> int
Cosmos-SDK's banker's rounding: https://github.com/cosmos/cosmos-sdk/blob/1d75e0e984e7132efd54c3526e36b3585e2d91c0/types/decimal.go#L491
Dec Objects
_1class Dec(JSONSerializable)
BigInt-based Decimal representation with basic arithmetic operations with
compatible Python numeric types (int, float, Decimal). Does not work with
NaN, Infinity, +0, -0, etc. Serializes as a string with 18 points of
decimal precision.
>>> Dec(5) Dec("5.0") >>> Dec("121.1232") Dec("121.1232") >>> Dec(121.1232) Dec("121.1232")
Arguments:
argUnion[str, int, float, Decimal, Dec] - argument to coerce into Dec
zero
_2@classmethod_2def zero(cls) -> Dec
Dec representation of zero.
Returns:
Dec- zero
one
_2@classmethod_2def one(cls)
Dec representation of one.
Returns:
Dec- one
__str__
_1def __str__() -> str
Converts to a string using all 18 decimal precision points.
Returns:
str- string representation
to_short_str
_1def to_short_str() -> str
Converts to a string, but truncates all unnecessary zeros.
Returns:
str- string representation
parity
_2@property_2def parity() -> int
Get the parity of the Dec value. Returns -1 if value is below 0, and 1 otherwise.
Returns:
int- parity
whole
_2@property_2def whole() -> str
Get the integral part of the Dec value.
Returns:
str- integer, as string
frac
_2@property_2def frac() -> str
Get the fractional part of the Dec value.
Returns:
str- fraction, as string
lt
_1def lt(other: Union[str, int, float, Decimal, Dec]) -> bool
Check less than.
Arguments:
otherUnion[str, int, float, Decimal, Dec] - compared object
le
_1def le(other: Union[str, int, float, Decimal, Dec]) -> bool
Check less than or equal to.
Arguments:
otherUnion[str, int, float, Decimal, Dec] - compared object
gt
_1def gt(other: Union[str, int, float, Decimal, Dec]) -> bool
Check greater than.
Arguments:
otherUnion[str, int, float, Decimal, Dec] - compared object
ge
_1def ge(other) -> bool
Check greater than or equal to.
Arguments:
otherUnion[str, int, float, Decimal, Dec] - compared object
add
_1def add(addend: Union[str, int, float, Decimal, Dec]) -> Dec
Performs addition. addend is first converted into Dec.
Arguments:
addendUnion[str, int, float, Decimal, Dec] - addend
Returns:
Dec- sum
sub
_1def sub(subtrahend: Union[str, int, float, Decimal, Dec]) -> Dec
Performs subtraction. subtrahend is first converted into Dec.
Arguments:
subtrahendUnion[str, int, float, Decimal, Dec] - subtrahend
Returns:
Dec- difference
mul
_1def mul(multiplier: Union[str, int, float, Decimal, Dec]) -> Dec
Performs multiplication. multiplier is first converted into Dec.
Arguments:
multiplierUnion[str, int, float, Decimal, Dec] - multiplier
Returns:
Dec- product
div
_1def div(divisor: Union[str, int, float, Decimal, Dec]) -> Dec
Performs division. divisor is first converted into Dec.
It works like truediv('/')
Arguments:
divisorUnion[str, int, float, Decimal, Dec] - divisor
Raises:
ZeroDivisionError- ifdivisoris 0
Returns:
Dec- quotient
mod
_1def mod(modulo: Union[str, int, float, Decimal, Dec]) -> Dec
Performs modulus. modulo is first converted into Dec.
Arguments:
moduloUnion[str, int, float, Decimal, Dec] - modulo
Returns:
Dec- modulus
from_data
_2@classmethod_2def from_data(cls, data: str) -> Dec
Converts Dec-formatted string into proper :class:Dec object.
with_prec
_2@classmethod_2def with_prec(cls, i: Union[int, str], prec: int) -> Dec
Replicates Cosmos SDK's Dec.withPreic(i, prec).
Arguments:
iUnion[int, str] - numeric valueprecint - precision
Returns:
Dec- decimal
Numeric Objects
_1class Numeric()
parse
_2@staticmethod_2def parse(value: Numeric.Input) -> Numeric.Output
Parses the value and coerces it into an int or :class:Dec.
Arguments:
valueNumeric.Input - value to be parsed
Raises:
TypeError- if supplied value could not be parsed
Returns:
Numeric.Output- coerced number