Integer
An Integer represents a whole number, stored as a signed 64-bit integer.
let x be 1There are several ways to represent an integer in Sparkle:
- In decimal:
69420 - In hexadecimal:
0x10f2c - In octal:
0o207454 - In binary:
0b10000111100101100
Alternatively, the built-in hex and binary functions with strings
can be used instead of 0x and 0b.
let a be hex "10f2c" # 0x10f2c
let b be binary "10000111100101100" # 0b10000111100101100In decimal, underscores are ignored, e.g. 69_420_000.
For numbers that are too large to fit on a signed 64-bit integer, refer to the BigInteger class.
Arithmetic
Integers can be used with arithmetic operators to perform numeric calculations. Note that divided by (or /) always produces a Float. For integer
divisions, use divided whole by (or //) instead.
let some_number be 12 plus 3 times 4 minus 5 raised to 2
print some_number # -1
print 5 divided by 2 # 5 / 2 = 2.5
print 5 divided whole by 2 # 5 // 2 = 2Operations between an Integer and a Float always produces a Float.
print 5 times 2.0 # 10.0Conversion
For convenience, you may use the built-in 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.01Constructor
Creates a new Integer by parsing the given value.
let a be new Integer with just "42"Getters
A getter that returns an Iterator to iterate from 0 up to the value
of the integer.
for each i in 10 do
print iMethods
Returns a string representation of the integer, in the given base.
let a be 69420
print a's to_string 16 # 10f2cReturns a string representation of the number in JSON format.
Returns a copy of itself.
Returns a copy of itself.
Returns a copy of itself.
Returns this integer clamped between min and max, inclusive.
let a be -1024
let b be a's to_clamped with 100, and 1000
print b # 100print "hello world"