Data type conversions can be performed explicitly to force the use of a specific data type for calculation. This can be done using one of the following functions.
|
Type cast | Description |
boolean(Integer) | cast to Boolean |
integer(Real) | cast to Integer |
real(Integer) | cast to Real |
string(Integer, Real, Complex, Quantity, or Boolean) | cast to String |
A typical example is to cast the value of an environment variable, which is usually given as a String, to an Integer value, e.g.,
a = "3.56"; b = integer(a); // -> 3 c = real(a); // -> 3.56 d = string(b + c); // -> "6.56"
In the example above the variable a is a String. However, the variable b contains the Integer and variable c the Real representation. When evaluating variable d, the sum of the values of the variables b and c will be calculated and converted to a String.
Some additional functions might be useful for operations with Complex or Quantity numbers:
|
Function | Description |
value(Complex, Quantity) | value without a unit |
unit(Complex, Quantity) | unit of a Quantity (String) |
realpart(Complex, Quantity) | real part of a Complex number |
imagpart(Complex, Quantity) | imaginary part of a Complex number |
value() removes the unit of a Quantity and returns only its value which can be a Real or a Quantity. realpart() and imagpart() can be used to extract the real and the imaginary part of a Complex number which can be a Quantity too (the value returned has the same unit as the argument given). Finally, unit() returns the unit of a Quantity as a String. Some examples:
a = (3.1 + 4.2 j) * 1 A; // Remove the unit: b = value(a); // -> 3.1 + 4.2 j // Get the real part of a quantity: c = realpart(a); // -> 3.1 A // Get the imaginary part of a complex number: d = imagpart(b); // -> 4.2 // Get the unit: f = unit(a); // -> "A"
Robert Klima 2003-02-06