int, double, bool. Transforming objects into numbers or bool

^name.int[]
or
^name.int(default)
^name.double[]
or
^name.double(default)
^name.bool[]
or
^name.bool(default)


The method transforms value of variable
$name into either integer or real number or bool respectively and returns it. If real number is transformed into integer, it will be truncated.

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).

One may also specify default value, which will be returned if transformation is impossible. Default value may be used when you process data received from visitors interactively. It will prevent text values from appearing in mathematical expressions, when a user inputs, for example, a string instead of a number initially expected.

Method
bool can convert into 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
Code…

$str[Item]
^str.int(1024)


…will output number
1024, as object str cannot be transformed into object of class int.

Code…

$double(1.5)
^double.int[]


…will output number
1, as the number was truncated.


Code...
^if(^form:search_in_text.bool(false){
   
...searching in text...
}



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