Skip to main content

terra_sdk.core.numeric

Numeric types.

DEC_NUM_DIGITS

Number of digits for Decimal.

chop_precision_and_round


_1
def 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


_1
class 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:

  • arg Union[str, int, float, Decimal, Dec] - argument to coerce into Dec

zero


_2
@classmethod
_2
def zero(cls) -> Dec

Dec representation of zero.

Returns:

  • Dec - zero

one


_2
@classmethod
_2
def one(cls)

Dec representation of one.

Returns:

  • Dec - one

__str__


_1
def __str__() -> str

Converts to a string using all 18 decimal precision points.

Returns:

  • str - string representation

to_short_str


_1
def to_short_str() -> str

Converts to a string, but truncates all unnecessary zeros.

Returns:

  • str - string representation

parity


_2
@property
_2
def 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
_2
def whole() -> str

Get the integral part of the Dec value.

Returns:

  • str - integer, as string

frac


_2
@property
_2
def frac() -> str

Get the fractional part of the Dec value.

Returns:

  • str - fraction, as string

lt


_1
def lt(other: Union[str, int, float, Decimal, Dec]) -> bool

Check less than.

Arguments:

  • other Union[str, int, float, Decimal, Dec] - compared object

le


_1
def le(other: Union[str, int, float, Decimal, Dec]) -> bool

Check less than or equal to.

Arguments:

  • other Union[str, int, float, Decimal, Dec] - compared object

gt


_1
def gt(other: Union[str, int, float, Decimal, Dec]) -> bool

Check greater than.

Arguments:

  • other Union[str, int, float, Decimal, Dec] - compared object

ge


_1
def ge(other) -> bool

Check greater than or equal to.

Arguments:

  • other Union[str, int, float, Decimal, Dec] - compared object

add


_1
def add(addend: Union[str, int, float, Decimal, Dec]) -> Dec

Performs addition. addend is first converted into Dec.

Arguments:

  • addend Union[str, int, float, Decimal, Dec] - addend

Returns:

  • Dec - sum

sub


_1
def sub(subtrahend: Union[str, int, float, Decimal, Dec]) -> Dec

Performs subtraction. subtrahend is first converted into Dec.

Arguments:

  • subtrahend Union[str, int, float, Decimal, Dec] - subtrahend

Returns:

  • Dec - difference

mul


_1
def mul(multiplier: Union[str, int, float, Decimal, Dec]) -> Dec

Performs multiplication. multiplier is first converted into Dec.

Arguments:

  • multiplier Union[str, int, float, Decimal, Dec] - multiplier

Returns:

  • Dec - product

div


_1
def div(divisor: Union[str, int, float, Decimal, Dec]) -> Dec

Performs division. divisor is first converted into Dec. It works like truediv('/')

Arguments:

  • divisor Union[str, int, float, Decimal, Dec] - divisor

Raises:

  • ZeroDivisionError - if divisor is 0

Returns:

  • Dec - quotient

mod


_1
def mod(modulo: Union[str, int, float, Decimal, Dec]) -> Dec

Performs modulus. modulo is first converted into Dec.

Arguments:

  • modulo Union[str, int, float, Decimal, Dec] - modulo

Returns:

  • Dec - modulus

from_data


_2
@classmethod
_2
def from_data(cls, data: str) -> Dec

Converts Dec-formatted string into proper :class:Dec object.

with_prec


_2
@classmethod
_2
def with_prec(cls, i: Union[int, str], prec: int) -> Dec

Replicates Cosmos SDK's Dec.withPreic(i, prec).

Arguments:

  • i Union[int, str] - numeric value
  • prec int - precision

Returns:

  • Dec - decimal

Numeric Objects


_1
class Numeric()

parse


_2
@staticmethod
_2
def parse(value: Numeric.Input) -> Numeric.Output

Parses the value and coerces it into an int or :class:Dec.

Arguments:

  • value Numeric.Input - value to be parsed

Raises:

  • TypeError - if supplied value could not be parsed

Returns:

  • Numeric.Output - coerced number