Quirrel4.41.0

types.String.tointeger

Method on every string
pure string.tointeger([base: number], ...): any

Parses str as an integer.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

basenumberthe numeric base to parse in, from 2 to 36; defaults to 10 optional
...anynot a real parameter; see Notes repeats

Return value

str parsed as an integer in the given base.

Errors

Throws cannot convert the string to integer when str does not parse as a number in base.

Notes

Takes 0 or 1 argument, not the 0-or-more the VM's dump implies. This binding carries no declaration string, so the VM cannot tell an optional parameter from a variadic tail and dumps it as tointeger([arg1: number], ...). A second explicit argument is silently ignored, since the underlying C++ only reads one extra argument.

Example

examples/types/string/tointeger.nut
println("\"42\".tointeger() =", "42".tointeger())
println("\"2a\".tointeger(16) =", "2a".tointeger(16))

// a second explicit argument beyond base is silently ignored, not an error.
println("\"2a\".tointeger(16, 999) =", "2a".tointeger(16, 999))

try { "xyz".tointeger() } catch (e) println("\"xyz\".tointeger() throws:", e)
Output:
"42".tointeger() = 42
"2a".tointeger(16) = 42
"2a".tointeger(16, 999) = 42
"xyz".tointeger() throws: cannot convert the string to integer

See also

tofloatParses str as a float.
stringclass index