ESC
Type to search...
S
Soli Docs

Floats

64-bit floating-point numbers (IEEE 754) with methods for rounding, comparison, and type conversion.

Creating Floats

pi = 3.14159
temp = -40.0
tiny = 0.001
sci = 2.5e10          # Scientific notation
let typed: Float = 1.5

Rounding Methods

.round / .round(digits)

Round to the nearest integer (0 args) or to a specific number of decimal places. Half-values are rounded away from zero (Ruby semantics).

3.7.round        # 4 (returns int)
3.14159.round(2) # 3.14 (returns float)
2.5.round        # 3
38.995.round(2)  # 39.0  — rounded via the shortest decimal
                 #          representation, so binary-float artifacts
                 #          like 38.995 == 38.99499999... don't bite
12345.0.round(-2) # 12300.0  — negative digits round to powers of 10

For exact decimal arithmetic (currency, etc.), prefer the Decimal type.

.ceil

Round up to the nearest integer.

3.2.ceil     # 4
(-3.2).ceil  # -3
.floor

Round down to the nearest integer.

3.7.floor    # 3
(-3.7).floor # -4
.truncate

Truncate toward zero (remove decimal part).

3.7.truncate     # 3
(-3.7).truncate  # -3
.abs

Returns the absolute value.

(-3.14).abs  # 3.14
5.0.abs      # 5.0
.sqrt

Returns the square root.

16.0.sqrt    # 4.0
2.0.sqrt     # 1.4142135623730951

Predicate Methods

.zero? / .positive? / .negative?

Check if the float is zero, positive, or negative.

0.0.zero?        # true
3.14.positive?   # true
(-1.5).negative? # true
.finite? / .infinite? / .nan?

Check for special IEEE 754 float states.

3.14.finite?     # true
3.14.infinite?   # false
3.14.nan?        # false
.between?(min, max)

Check if the float is within the given range (inclusive).

3.14.between?(1.0, 5.0)   # true
0.5.between?(1.0, 5.0)    # false
.clamp(min, max)

Constrain the float to be within the given range.

15.5.clamp(0.0, 10.0)  # 10.0
(-5.0).clamp(0.0, 10.0) # 0.0

Timing Methods

.sleep

Pauses the current thread for the float number of seconds (sub-second precision). Negative, NaN, or infinite values are a no-op. Returns null.

0.5.sleep       # pause for 500ms
0.01.sleep      # pause for 10ms
delay = 1.5
delay.sleep     # pause for 1.5 seconds

Conversion Methods

.to_i / .to_int

Convert to integer (truncates toward zero).

3.7.to_i     # 3
(-3.7).to_int # -3
.to_s / .to_string

Convert to string representation.

3.14.to_s    # "3.14"
(-1.5).to_string  # "-1.5"

Introspection Methods

.class

Returns the type name as a string.

3.14.class   # "float"
.inspect

Returns a developer-friendly string representation.

3.14.inspect # "3.14"
.is_a?(type_name)

Check if the value is of the given type. Supports "float", "numeric", and "object".

3.14.is_a?("float")     # true
3.14.is_a?("numeric")   # true
3.14.is_a?("int")       # false
.nil? / .blank? / .present?

Floats are never nil and always present.

3.14.nil?      # false
3.14.blank?    # false
3.14.present?  # true