Skip to main content
Log in

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:

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

fn __round__(self) -> Self:
return Self(round(re), round(im))
@value
struct Complex(Roundable):
var re: Float64
var im: Float64

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

Implemented traits​

AnyType

Methods​

__round__​

__round__(self: T) -> T

Get a rounded value for the type.

Returns:

The rounded value.

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

Get a rounded value for the type.

Args:

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

Returns:

The rounded value.

Was this page helpful?