Type Conversions
In certain contexts, values from one data type can be converted to another data type, either implicitly or explicitly.
From Text Values to Boolean Values
The text values of "true" and "false" (either in all lower case, all upper case, or initial capitalization) can be used for setting the value of Boolean fields, when enclosed in single or double quotes.
The text values of "n" (where n represents an integer or decimal value, and can be positive or negative), can be used for setting the value of logical fields, when enclosed in single or double quotes. If n is 0, the corresponding Boolean value is false; otherwise, all other values of n correspond to a Boolean value of true.
When creating logical comparisons (for example, for the condition in the iif() expression), only the text values of true and false (strictly in all lower case) can be used for setting the value of Boolean fields. Text values containing numerical values cannot be properly used in Boolean comparisons.
From Number Values to Boolean Values
Number values can also be used for setting the value of Boolean fields, when enclosed in single or double quotes.
The number zero (0) is converted to the Boolean value of false; otherwise, all other number values (either positive or negative, integer or decimal) are converted to the Boolean value of true.
Number values cannot be properly used in logical comparisons.
Text values representing the date only or representing date and time values (see Constructing Expressions), can be implicitly converted to the corresponding DateTime value, and subsequently used to compare against other DateTime values (such as the values from DateTime fields).
Integer values can be implicitly converted to decimal values, but not vice versa.
From Boolean Values to Number Values
The Boolean values of true and false can be used for setting the value of number fields, when in all lower or all upper case. Upon evaluation, the true and false values are converted to 1 and 0, respectively.
Boolean values, however, cannot be implicitly converted to number values, when used in arithmetic contexts (for example, adding a number value to true), or in relational contexts (for example, comparing a number value to true).
From Text Values to Number Values
There is no implicit or explicit conversion of text values to number values.
For arithmetic operations, if one operand is a text value, and the other operand is a number value, the number value is converted to a text value, but the text value is not converted to a number value. At this point, only the plus (+) operator (in this context, for text concatenation) works properly; evaluation fails for all other available arithmetic operators (such as multiply (*) and divide (%)).
Similarly, for relational comparisons, if one operand is a text value, and the other operand is a number value, the number value is converted to a text value, but the text value is not converted to a number value. At this point, the values are compared lexicographically instead of numerically.
Therefore, the integer or decimal number values must be specified as is, without the enclosing single or double quote characters, so that they can be used in the proper contexts.
Boolean and number values can be implicitly converted to text values, when used in the context of text concatenation. The non-text value is first converted to a text value, which is then concatenated with the other text values in the expression.
From Boolean Values to Text Values
For Boolean values, true and false are converted to the text values "true" and "false", respectively (notice the values are in lower case).
Here are examples where the Boolean value is used, in the context of text concatenation (here, TestText1 is a text field, whereas TestBool1 is a logical field):
$("Did the user answer all the questions: " + false)
$("Did the user answer all the questions: " + TestBool1)
$(TestText1 + TestBool1)
From DateTime Values to Text Values
DateTime values cannot be implicitly converted to text values through text concatenation, but you can use the ToString function to obtain the text equivalent.
To illustrate the distinction, when the following expression is evaluated, here, TestDate1 stores a DateTime value:
$("Liftoff will occur at " + TestDateTime1)
the following error occurs:
Error while saving data: DataValue.PromoteToCommonType: DateTime: cannot convert from string
This error occurs for both time-only and DateTime fields.
For date-only fields, the following error occurs instead:
Error while saving data: DataValue.PromoteToCommonType: unexpected second argument type: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
To properly convert the DateTime value to text, use the ToString function. The function takes the DateTime value in UTC, and converts the value to a text value.
With the above expression updated to use the ToString function,
$("Liftoff will occur at " + ToString(TestDateTime1))
the user receives the following result:
Liftoff will occur at 7/16/2013 9:30:00 AM
For time-only fields, the ToString function returns a DateTime value. The time portion is in UTC, whereas the date portion is "1/1/1970".
For example, after evaluating the following expression:
$("Liftoff will occur at " + ToString(TestTime1))
the user receives the following result:
Liftoff will occur at 1/1/1970 9:30:00 AM
For date-only fields, even when using the ToString function, the ToString value is not able to properly convert the date-only value to a text value, and the evaluation of the expression silently returns an error value.
For example, after evaluating the following expression:
$("Liftoff will occur at " + ToString(TestDate1))
the user receives the following erroneous result:
Liftoff will occur at System.Collections.Generic.Dictionary2[System.String,System.Object]
From Number Values to Text Values
Here are examples where the number value is used in the context of text concatenation. Here, TestText1 and TestText2 are text fields, whereas TestNumber is a number field.
$("There are >" + 3 + " blind mice")
$("There are " + 3.28 + TestText1 )
$(TestText1 + TestNumber + TestText2)