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(self.re), round(self.im))
fn __round__(self, ndigits: Int) -> Self:
return Self(round(self.re, ndigits), round(self.im, ndigits))
@value
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
Methods
__round__
__round__(self: _Self) -> _Self
Get a rounded value for the type.
Returns:
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:
The rounded value.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!