Skip to main content

Mojo trait

Roundable

The Roundable trait describes a type that defines a rounding operation.

Types that conform to Roundable will work with the builtin round function. The round operation always returns the same type as the input.

For example:

@fieldwise_init
struct Complex(Roundable):
    var re: Float64
    var im: Float64

    fn __round__(self) -> Self:
        return Self(round(self.re), round(self.im))

    fn __round__(self, ndigits: Int) -> Self:
        return Self(round(self.re, ndigits), round(self.im, ndigits))

Implemented traits

AnyType, UnknownDestructibility

Aliases

__del__is_trivial

alias __del__is_trivial

A flag (often compiler generated) to indicate whether the implementation of __del__ is trivial.

The implementation of __del__ is considered to be trivial if:

  • The struct has a compiler-generated trivial destructor and all its fields have a trivial __del__ method.

In practice, it means that the __del__ can be considered as no-op.

Required methods

__round__

__round__(self: _Self) -> _Self

Get a rounded value for the type.

Returns:

_Self: The rounded value.

__round__(self: _Self, ndigits: Int) -> _Self

Get a rounded value for the type.

Args:

  • ndigits (Int): Number of digits after the decimal point.

Returns:

_Self: The rounded value.

Was this page helpful?