Conversion Between Data Types

  Previous  Next

Introduction

If there are different data types within an expression, they must be converted into the same type. MADRIX Script does those conversions implicitly, but in most cases a warning will be displayed in the Compiler Messages section of the Script Editor. It is also possible to do those conversions explicitly writing the destination data type in brackets before the expression, like this:

int i = (int)GetSpeedPitch();                //if the Pitch was 6.2, i is now 6
string s = (string)i;                //s now consists of the characters "6"
 
 

GetSpeedPitch() returns a float value which has to be converted into int before it can be assigned to i. Be aware that the positions after the decimal point are abridged. Afterwards, the numeric value is assigned to i.

The following table shows an overview of the possible conversions:

 

int

float

string

structure

bool

expressions

int

-

Converts the value to float.

Converts the value to string: e.g. 12 = "12".

N/A

Is true if the value is not 0.

float

Converts the value to int. The decimal part is truncated without rounding off.

-

Converts the value to string: e.g. 12.34 = "12.34"

N/A

Is true if the value is not 0.

string

If the string starts with a number, it is converted into an integer/float value, otherwise  the result is 0.

-

N/A

Is true if the string is not empty.

structure

N/A

Conversion between different structures: N/A.

N/A

 

Implicit Conversions Within Expressions

If one of the two operands of an expression is of the type float and the other is of the type int, the int value is converted to float and the expression results in the data type float.

If one of the two operands of an expression is of the type string, the other operand is converted into a string and the expression results in a string.

If the conversion is not possible according to the table above, the compiler prints an error and you have to correct the expression.

Here are some examples of expressions and their results:

Expression

Result

Explanation

int i = 3 / 4 * 2

0

3 / 4 is an integer operation and results in 0. Hence, the whole expression results in 0.

float f = 3 / 4.0 * 2

1.5

Because 4.0 is a float value, 3 is converted into float, too. This way, 3 / 4.0 results in 0.75. Then, 2 is also converted into float since the other operand is of type float, and the result is therefore 1.5.

string test = "It is " + 9 + " o'clock."

"It is 9 o'clock."

Since the first operand is a string, 9 is also converted into a string and concatenated with the first string.

string test = 2 + 3 + "40"

"540"

2 and 3 are both integer values and will be added up due to an integer operation. The second operation has a string as operand and therefore the other operand is converted into a string and both are concatenated together.

 

MADRIX Version: 5.7 | Script Version: 3.23
[Ctrl & +/-] = Zoom In/Out | [Ctrl & 0] = 100%
 Previous   Next

 


Enable automatic translation | Activer la traduction automatique |