Quirrel4.41.0

types.Float.tointeger

Method on every float
pure (bool|number).tointeger(): any

Converts the float to an integer.

Return value

The value truncated toward zero, not rounded: (3.9).tointeger() gives 3 and (-3.9).tointeger() gives -3, both dropping the fractional part rather than rounding to the nearest integer.

Errors

Nothing throws for a value outside the 64-bit integer range, but the result is then implementation-defined rather than meaningful: converting a float too large (or too negative) to fit a SQInteger is a plain C++ (SQInteger) cast on an out-of-range value, which is undefined behavior in the language standard. On this build's target it happens to saturate to the same fixed sentinel (-9223372036854775808, i.e. INT64_MIN) for every such float, whether the overflow is positive or negative, but that is a property of the compiler and CPU, not a guarantee - do not rely on the exact value.

Notes

Takes no arguments, same as types.Integer.tointeger; no base parameter exists here either, unlike types.String.tointeger.

Example

examples/types/float/tointeger.nut
println("(3.9).tointeger() =", (3.9).tointeger())   // truncates toward zero, it does not round
println("(-3.9).tointeger() =", (-3.9).tointeger())
println("type((3.9).tointeger()) =", type((3.9).tointeger()))

// no base argument here either, unlike string's tointeger(base)
try { (3.9).tointeger(10) }
catch (e) println("(3.9).tointeger(10) throws:", e)
Output:
(3.9).tointeger() = 3
(-3.9).tointeger() = -3
type((3.9).tointeger()) = integer
(3.9).tointeger(10) throws: wrong number of parameters passed to native closure 'tointeger' (2 passed, 1 required)

See also

tointegerReturns the integer itself.
tofloatReturns the float itself.
tocharConverts the float to a one-character string, treating it as a character code.
tointegerConverts the bool to an integer.
floatclass index