In most cases you can safely rely on Cows' automatic type conversion. However, there are situations where you must force a conversion to obtain the right result. As an example, when you multiply a string by an integer n the result is the string repeated n times ("pamela" * 3 is "pamelapamelapamela"). For the same reason, "4" * 3 gives the string "444" as a result. So, if a variable stores the string "4" and you want a standard multiplication of two integers, you need a conversion:
x = 4; print (x * 3); // display 444 print (toint (x) * 3); // display 12
Please note that the original variable is left unchanged: toint (x
) is an integer but x
is still a
string. In the same spirit y
= toint
(x
) will create the integer variable y but x will still
be a string.
if expr
is a boolean or an integer, it is returned
unchanged (remember that booleans are implemented as integers);
if expr
is a string representing a number (e.g. "12") the
corresponding integer is returned;
arrays can't be converted into integers.
if expr
is a boolean or an integer, a string representing
the integer is returned;
if expr
is a string it is returned unchanged;
if expr
is an array, a string listing its elements is
returned.
if expr
is a boolean, an integer or a string, an array
with expr
as its only element is returned;
if expr
is an array it is returned unchanged.
This manual can be downloaded from http://www.g-cows.org/.