Numeric Types

class terra_sdk.core.numeric.Numeric[source]
Input

alias of Union[str, int, float, decimal.Decimal, terra_sdk.core.numeric.Dec]

Output

alias of Union[int, terra_sdk.core.numeric.Dec]

static parse(value)[source]

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

Parameters

value (Numeric.Input) – value to be parsed

Raises

TypeError – if supplied value could not be parsed

Returns

coerced number

Return type

Numeric.Output

Integers

Terra SDK uses Python’s native int type to capture both native numbers like uint8, as well as Cosmos SDK’s sdk.Int which is normally coerced into a string as it must be passed in JSON format. The Python’s int provides support for BigNumber implementation for artihmetic operations.

Warning

It is possible to introduce numbers larger than 256-bit precision allowed by Terra blockchain but they will result in an error when processing.

Decimals

class terra_sdk.core.Dec(arg)[source]

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")
Parameters

arg (Union[str, int, float, Decimal, Dec]) – argument to coerce into Dec

add(addend)[source]

Performs addition. addend is first converted into Dec.

Parameters

addend (Union[str, int, float, Decimal, Dec]) – addend

Returns

sum

Return type

Dec

div(divisor)[source]

Performs division. divisor is first converted into Dec.

Parameters

divisor (Union[str, int, float, Decimal, Dec]) – divisor

Raises

ZeroDivisionError – if divisor is 0

Returns

quotient

Return type

Dec

property frac: str

Get the fractional part of the Dec value.

Returns

fraction, as string

Return type

str

classmethod from_data(data)[source]

Converts Dec-formatted string into proper Dec object.

Return type

Dec

ge(other)[source]

Check greater than or equal to.

Parameters

other (Union[str, int, float, Decimal, Dec]) – compared object

Return type

bool

gt(other)[source]

Check greater than.

Parameters

other (Union[str, int, float, Decimal, Dec]) – compared object

Return type

bool

le(other)[source]

Check less than or equal to.

Parameters

other (Union[str, int, float, Decimal, Dec]) – compared object

Return type

bool

lt(other)[source]

Check less than.

Parameters

other (Union[str, int, float, Decimal, Dec]) – compared object

Return type

bool

mod(modulo)[source]

Performs modulus. modulo is first converted into Dec.

Parameters

modulo (Union[str, int, float, Decimal, Dec]) – modulo

Returns

modulus

Return type

Dec

mul(multiplier)[source]

Performs multiplication. multiplier is first converted into Dec.

Parameters

multiplier (Union[str, int, float, Decimal, Dec]) – multiplier

Returns

product

Return type

Dec

classmethod one()[source]

Dec representation of one.

Returns

one

Return type

Dec

property parity: int

Get the parity of the Dec value. Returns -1 if value is below 0, and 1 otherwise.

Returns

parity

Return type

int

sub(subtrahend)[source]

Performs subtraction. subtrahend is first converted into Dec.

Parameters

subtrahend (Union[str, int, float, Decimal, Dec]) – subtrahend

Returns

difference

Return type

Dec

to_data()[source]

Converts the object to its JSON-serializable Python data representation.

Return type

str

to_short_str()[source]

Converts to a string, but truncates all unnecessary zeros.

Returns

string representation

Return type

str

property whole: str

Get the integral part of the Dec value.

Returns

integer, as string

Return type

str

classmethod with_prec(i, prec)[source]

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

Parameters
  • i (Union[int, str]) – numeric value

  • prec (int) – precision

Returns

decimal

Return type

Dec

classmethod zero()[source]

Dec representation of zero.

Returns

zero

Return type

Dec