ToInteger (open)
Returns an integer representation of a string
Syntax
// Core function, no LOADLIB necessary
INTEGER FUNCTION ToInteger(STRING str, INTEGER defaultvalue, INTEGER radix)
Parameters
STRING str
String to convert
INTEGER defaultvalue
Value to return if 'str' is not a valid number in the specified number system
INTEGER radix
Number system used in the string, ranging from 2 to 36. If the base is out of range, defaultvalue is returned. This parameter is optional and defaults to 10 (decimal system)
Return value
INTEGER
The number in the passed string as an integer, or defaultvalue if the conversion failed.
Description
ToInteger parses the string, and attempts to return an integer of the specified base (radix). If the function failed to parse the specified string, it will return @italic defaultvalue
A base of 10 indicates that a decimal number should be converted, 8 octal, 16 hexadecimal, and so on.
Examples
// The following examples all return 15
INTEGER example1 := ToInteger("15", -1);
INTEGER example2 := ToInteger("15", -1, 10);
INTEGER example3 := ToInteger("F", -1, 16);
INTEGER example4 := ToInteger("1111", -1, 2);
// The following faulty strings all return -1
INTEGER example5 := ToInteger("15,99", -1);
INTEGER example6 := ToInteger("5*3", -1, 10);
INTEGER example7 := ToInteger("FFFFFFFFFF", -1, 16);
INTEGER example8 := ToInteger("22", -1, 2);