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
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).

Floatable

The Floatable trait describes a type that can be converted to a Float64.

This trait requires the type to implement the __float__() method.

For example:

@fieldwise_init
struct Foo(Floatable):
var i: Float64

def __float__(self) -> Float64:
return self.i

A Foo can now be converted to a Float64:

@fieldwise_init
struct Foo(Floatable):
var i: Float64

def __float__(self) -> Float64:
return self.i

var f = Float64(Foo(5.5))

Note: If the __float__() method can raise an error, use the FloatableRaising trait instead.

Implemented traits

AnyType

Required methods

__float__

__float__(self: _Self) -> Float64

Get the float point representation of the value.

Returns:

Float64: The float point representation of the value.