Quirrel4.41.0

types.Integer.tointeger

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

Returns the integer itself.

Return value

this, unchanged. Provided so every numeric type answers the same tointeger/tofloat pair without the caller checking which one it already has.

Notes

Takes no arguments, and that arity is fixed: unlike types.String.tointeger, which takes an optional base for parsing digits out of text, the number-typed tointeger() has nothing left to parse and was registered with no room for an extra argument. (5).tointeger(16) throws a wrong-number-of-parameters error rather than silently ignoring the base.

See types.Float.tointeger for what happens when the value being converted has a fractional part, or is too large to fit in a 64-bit integer.

Example

examples/types/integer/tointeger.nut
// tointeger() on an integer is the identity conversion.
println("(42).tointeger() =", (42).tointeger())

// Unlike string.tointeger(base), the number version takes no base argument:
// this closure was registered with a fixed arity of zero real parameters.
try { (42).tointeger(16) }
catch (e) println("(42).tointeger(16) throws:", e)

println("\"2A\".tointeger(16) =", "2A".tointeger(16)) // string's own tointeger *does* take a base
Output:
(42).tointeger() = 42
(42).tointeger(16) throws: wrong number of parameters passed to native closure 'tointeger' (2 passed, 1 required)
"2A".tointeger(16) = 42

See also

tofloatConverts the integer to a float.
tocharConverts the integer to a one-character string, treating it as a character code.
tointegerConverts the float to an integer.
tointegerConverts the bool to an integer.
integerclass index