Skip to content

float

The float module is auto-imported — no import statement needed.

FunctionSignatureDescription
to_string(Float) -> StringConvert a float to its string representation.
to_int(Float) -> IntTruncate a float to an integer (rounds toward zero).
round(Float) -> FloatRound a float to the nearest integer value (as Float).
floor(Float) -> FloatRound a float down to the nearest integer value (as Float).
ceil(Float) -> FloatRound a float up to the nearest integer value (as Float).
abs(Float) -> FloatReturn the absolute value of a float.
sqrt(Float) -> FloatReturn the square root of a float.
parse(String) -> Result[Float, String]Parse a string into a float. Returns err if the string is not a valid number.
from_int(Int) -> FloatConvert an integer to a float.
min(Float, Float) -> FloatReturn the smaller of two floats.
max(Float, Float) -> FloatReturn the larger of two floats.
to_fixed(Float, Int) -> StringFormat a float with a fixed number of decimal places.
clamp(Float, Float, Float) -> FloatClamp a float to the range [lo, hi].
sign(Float) -> FloatReturn the sign of a float: -1.0, 0.0, or 1.0.
is_nan(Float) -> BoolCheck if a float is NaN (not a number).
is_infinite(Float) -> BoolCheck if a float is positive or negative infinity.

Convert a float to its string representation.

float.to_string(3.14) // => \"3.14\

Truncate a float to an integer (rounds toward zero).

float.to_int(3.9) // => 3

Round a float to the nearest integer value (as Float).

float.round(3.6) // => 4.0

Round a float down to the nearest integer value (as Float).

float.floor(3.9) // => 3.0

Round a float up to the nearest integer value (as Float).

float.ceil(3.1) // => 4.0

Return the absolute value of a float.

float.abs(-2.5) // => 2.5

Return the square root of a float.

float.sqrt(9.0) // => 3.0

float.parse(s: String) -> Result[Float, String]

Section titled “float.parse(s: String) -> Result[Float, String]”

Parse a string into a float. Returns err if the string is not a valid number.

float.parse(\"3.14\") // => ok(3.14)

Convert an integer to a float.

float.from_int(42) // => 42.0

Return the smaller of two floats.

float.min(1.5, 2.5) // => 1.5

Return the larger of two floats.

float.max(1.5, 2.5) // => 2.5

float.to_fixed(n: Float, decimals: Int) -> String

Section titled “float.to_fixed(n: Float, decimals: Int) -> String”

Format a float with a fixed number of decimal places.

float.to_fixed(3.14159, 2) // => \"3.14\

float.clamp(n: Float, lo: Float, hi: Float) -> Float

Section titled “float.clamp(n: Float, lo: Float, hi: Float) -> Float”

Clamp a float to the range [lo, hi].

float.clamp(15.0, 0.0, 10.0) // => 10.0

Return the sign of a float: -1.0, 0.0, or 1.0.

float.sign(-3.5) // => -1.0

Check if a float is NaN (not a number).

float.is_nan(0.0 / 0.0) // => true

Check if a float is positive or negative infinity.

float.is_infinite(1.0 / 0.0) // => true

Reinterpret a float as its IEEE 754 bit representation (i64).

float.to_bits(1.0) // => 4607182418800017408