IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0b1
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

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

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

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

Implemented traits

AnyType

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.