int, double, bool. Converting string into number or bool

^string.int[]
^string.int(default value)
^string.double[]
^string.double(default value)
^string.bool[]
^string.bool(default value)

Converts value of variable
$string into integer or real number or bool respectively and returns the result.

One can specify default value to be returned if conversion is impossible, a string is empty or consists of white space characters (tabs, spaces, newlines).

Default value can be used while processing data, which is received from users interactively. It will help avoiding text values in mathematical expressions-in cases of incorrect input (e.g. when a string is received instead of expected number).

Note: method
.bool can convert to bool not only strings with numbers (0-false, not 0-true) but strings containing values 'true'/'false' as well (case insensitive). It can be usable for reading data from external source (xml for example).

Note: using empty string in mathematical expressions expressions is not considered error. Its value is then regarded as zero.

Note: attempt of converting non-integer string into integer is considered error (e.g. string "1.5" is not an integer).

Example

$str[123]
^str.int[]


…outputs number
123, since object str can be converted into number.

$str[much]
^str.double(-1)


…outputs number
-1, since conversion is impossible.


$str[1]
^if(^str.bool[]){
true}

$str[
True]
^if(^str.bool[]){
true}

…outputs strings "true".



Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 05.02.2008