int

int <string>

Parse a string as a base-10 integer.

This is the explicit string-to-int conversion. There is no implicit coercion from strings to numbers, so use int whenever you need to do arithmetic or numeric comparison on text — for example the words of $args or a value read back with recall.

Arguments

Returns

An int — the parsed value.

If the string does not parse as a base-10 integer, that is a run-time error and the execution stops (see control flow and the parse-failure rule in values). There is no fallback value; guard the input if it might not be numeric.

Examples

after command (say) {
  do "say [int "42"] is the answer."
}

[int "42"] evaluates to the integer 42, so the mob says "42 is the answer."

Convert text before arithmetic with the operators:

after command (say) {
  let count [int [first $args]]
  do "say Doubled, that is [* $count 2]."
}

The first word of the command line is parsed to an int, then doubled. If that word is not a number — say the player typed say applesint raises a run-time error and the handler stops.

See also