Skip to content

type_bridge.attribute.decimal

decimal

Decimal attribute type for TypeDB.

Decimal

Decimal(value)

Bases: NumericAttribute

Decimal attribute type that accepts fixed-point decimal values.

This maps to TypeDB's 'decimal' type, which is a fixed-point signed decimal number with 64 bits to the left of the decimal point and 19 decimal digits of precision after the point.

Range: −2^63 to 2^63 − 10^−19 (inclusive)

Example

from decimal import Decimal as DecimalType

class AccountBalance(Decimal): pass

class Price(Decimal): pass

Usage with decimal values

balance = AccountBalance(DecimalType("1234.567890")) price = Price(DecimalType("0.02"))

Initialize Decimal attribute with a decimal value.

Parameters:

Name Type Description Default
value Decimal | str | int | float

The decimal value to store. Can be: - decimal.Decimal instance - str that can be parsed as decimal - int or float (will be converted to Decimal)

required
Example

from decimal import Decimal as DecimalType

From Decimal

balance = AccountBalance(DecimalType("123.45"))

balance = AccountBalance("123.45")

From int or float (may lose precision)

balance = AccountBalance(123.45)

Source code in type_bridge/attribute/decimal.py
def __init__(self, value: DecimalType | str | int | float):
    """Initialize Decimal attribute with a decimal value.

    Args:
        value: The decimal value to store. Can be:
            - decimal.Decimal instance
            - str that can be parsed as decimal
            - int or float (will be converted to Decimal)

    Example:
        from decimal import Decimal as DecimalType

        # From Decimal
        balance = AccountBalance(DecimalType("123.45"))

        # From string (recommended for precision)
        balance = AccountBalance("123.45")

        # From int or float (may lose precision)
        balance = AccountBalance(123.45)
    """
    if not isinstance(value, DecimalType):
        value = DecimalType(str(value))
    super().__init__(value)

value property

value

Get the stored decimal value.