Float

An Float represents a decimal number, stored as a signed 64-bit floating point number.

let x be 3.14

There are several ways to represent a float in Sparkle:

  • Using a decimal point: 0.000123
  • Using scientific notation: 1.23e-4

Floats are not the same as Integers, for example 4.0 and 4 are not equal.

Arithmetic

Floats can be used with arithmetic operators to perform numeric calculations. Any mathematical operations involving at least one Float will also produce a Float.

let a be 3 plus 0.14   # 3.14
let b be 0.1 plus 0.2  # 0.30000000000000004

Floating-point arithmetic is approximate, keep this in mind when comparing numbers for equality! Read more here.

Conversion

For convenience, you may use the integer and float functions to easily convert a number or parse a string to your desired type.

let a be integer 3.14  # 3
let b be float 42      # 42.0
let c be integer "69"  # 69
let d be float "0.01"  # 0.01

Constructor

Creates a new Float by parsing the given value.

let a be new Float with just "42"

Methods

Returns a string representation of the number.

Returns a string representation of the number in JSON format.

Returns a string representation of the number using scientific notation (e-notation), rounded to the given amount of precision.

let a be 0.0123456
print a's to_scientific_notation 3 # 1.235e-2

Returns a copy of itself rounded to the nearest Integer.

Returns a copy of itself rounded down to the nearest Integer.

Returns a copy of itself rounded up to the nearest Integer.

Returns this float clamped between min and max, inclusive.

let a be 0.5
let b be a's to_clamped with 0.2, and 0.3

print b # 0.3
print "hello world"