Automatic data type conversion

LotusScript® can automatically convert values from one data type to another. Automatic, or implicit data type conversion happens when:

  • You assign a value of one numeric data type to a variable of a different numeric data type.

    LotusScript® converts the data type of the value being assigned to the data type of the variable to which it is being assigned, if possible. For example: aDouble# = anInteger% assigns the value of the integer variable anInteger% to the double floating-point variable aDouble#, with the necessary conversion taking place automatically.

  • You perform an arithmetic or comparison operation involving values of different numeric data types.

    When two numeric values with different data types are used as operands on either side of an arithmetic operator, LotusScript® converts the data type of one operand to the data type of the other operand before the operation is evaluated, if possible. For example: aVariantV = anInteger% + aDouble# adds the values of anInteger% and aDouble#, treating them both as values of type Double. The result is then assigned to a Variant variable of type Double.

    When you compare two values of different numeric data types, LotusScript® treats them as being of the same data type for the purpose of comparison. For example, the values of the variable anInt% and the variable myLong& are both treated as Long:

    If anInt% > myLong& Then
      Print "anInt% is greater than myLong&."
    End If
  • You increment the value of a Variant variable of some numeric type beyond the allowable limit for values of that type.

    For example, the statement aVariantV = aVariantV + 5 assigns a value of type Long, rather than a value of type Integer, to the Variant variable aVariantV because the largest value an Integer can have in LotusScript® is 32767:

    aVariantV = 32767
    Print TypeName(aVariantV)     ' Output: INTEGER
    aVariantV = aVariantV + 5
    Print TypeName(aVariantV)     ' Output: LONG 
  • You add or concatenate the values of two Variant variables, one of which is of type String and the other of which is one of the numeric data types.

    Addition is performed when one of the following is true:

    • Both operands contain numeric values.
    • One operand is numeric, and the other is a Variant containing a string that can be interpreted as a number.
    • Both operands are Variants, with a numeric value in one and a string value that can be interpreted as a number in the other.

    Concatenation is performed when one of the following is true:

    • Both operands are strings.
    • One operand is a string that can't be interpreted as a number, and the other is a Variant containing a numeric value.
Note: It is not always possible to convert values. If the conversion is not possible, a type mismatch error is raised.
Note: It is highly recommended that you use explicit conversion as much as possible to avoid unexpected results.

Example 1

' This example illustrates the automatic conversion
' of decimal numbers to integers that happens when you perform
' integer division and when you assign a decimal number value
' to an integer variable.
Dim anInt As Integer
Dim aDouble As Double
' Do floating-point division.
anInt% = 12/7
Print anInt%
' Output: 2
aDouble# = 12/7
Print aDouble#
' Output: 1.71428571428571
' Do integer division.
anInt% = 12\7
Print anInt%
' Output: 1
aDouble# = 12\7
Print aDouble#
' Output: 1

' Do floating-point division.
anInt% = 12.5/2
Print anInt%
' Output: 6
aDouble# = 12.5/2
Print aDouble#
' Output: 6.25

' Do integer division.
anInt% = 12.5\2
Print anInt%
' Output: 6
aDouble# = 12.5\2
Print aDouble#
' Output: 6

Example 2

In this example, the value 1.6 is assigned to X. Since X is a variable of type Integer, 1.6 is converted to an integer before the assignment takes place. Conversion of floating-point values (Single and Double values) to integer values (Integer and Long values) rounds the value to the nearest integer, which is 2 in this case.

When 1.5 is assigned to Y, LotusScript® rounds it to 2, the nearest even integer. A floating-point value exactly halfway between two integer values is always rounded to the nearest even integer value. So the value 2.5 is also rounded to 2 when it is assigned to Z. A value of 3.5 would be rounded to 4, a value of -3.5 would be rounded to -4, and so on. A value of .5 or -.5 is rounded to 0.

Dim X As Integer
Dim Y As Integer
Dim Z As Integer
X% = 1.6
Print X%
' Output: 2
Y% = 1.5
Print Y%
' Output: 2
Z% = 2.5
Print Z%
' Output: 2

Example 3

This example illustrates the way in which LotusScript® handles data type conversion in Variant variables to accommodate numeric values.

Dim sumV As Variant
Dim sInt As Integer
sInt% = 42
sumV = sInt% 
Print TypeName(sumV)
' Output: INTEGER
' Assign the largest integer value to sInt%.
sInt% = 32767
sumV = sInt% + 1
' LotusScript converts sumV to a Long to prevent 
' an overflow.
Print TypeName(SumV)
' Output: LONG

Example 4

This example shows how LotusScript® does number-to-string and string-to-number conversion when a Variant variable is an operand in an operation involving the + operator, which can be used for both addition and string concatenation.

Dim aVariantV As Variant
aVariantV = 1040
Print TypeName(aVariantV)
' Output: INTEGER
Print aVariantV + "A"
' Output: 1040A
' because "A" is a string and 1040 can be interpreted as a string.
aVariantV = "43"
Print TypeName(aVariantV)
' Output: STRING
Print aVariantV + 5
' Output: 48
' because 48 is a number and 5 can be interpreted as a number.