Skip to main content

Float

A floating point value, e.g. 3.14.

var number: Float = 3.14

Special Values

Infinity

Use the literal infinity to create a float with that value.

var max_width = infinity

NaN (Not a number)

This value marks an invalid floating point value. It can result from an invalid computation, e.g. 0/0. To create a float with that value use nan.

var not_a_number = nan

Properties

is_finite: Bool (get)

Whether the value is finite.

is_nan: Bool (get)

Whether the value is invalid.

Methods

ceiled() -> (Float)

Returns the float representation of the least integer not less than the value.

var positive = 3.14
var negative = -3.14
print("\{positive.ceiled()}") // Prints 4.0
print("\{negative.ceiled()}") // Prints -3.0

clamped(min: Float, max: Float) -> (Float)

Returns a value that restricts the given value within the given min and max range.

var a = 12.3
var b = a.clamped(min: 0.9, max: 9.9)
print ("\{b}") // Prints 9.9

floored() -> (Float)

Returns the float representation of the largest integer not greater than the value.

var positive = 3.14
var negative = -3.14
print("\{positive.floored()}") // Prints 3.0
print("\{negative.floored()}") // Prints -4.0

formatted(digits: Int) -> (String)

Returns a string representation of the value using fixed-point notation with the specified number of digits.

var value_1 = 0.5
var value_2 = 3.1415
print("\{value_1.formatted(digits: 2)}") // Prints "0.50"
print("\{value_2.formatted(digits: 2)}") // Prints "3.14"

rounded() -> (Float)

Returns the float representation of the nearest integer to the value, rounding half-way cases away from zero.

var positive = 3.14
var positive_2 = 3.5
var negative = -3.14
var negative_2 = -3.5
print("\{positive.rounded()}") // Prints 3.0
print("\{negative.rounded()}") // Prints -3.0
print("\{positive_2.rounded()}") // Prints 4.0
print("\{negative_2.rounded()}") // Prints -4.0

to_int() -> (Int)

Returns the integer part of the given float number.