LOTUSSCRIPT LANGUAGE


Automatic data type conversion
LotusScript can automatically convert values from one data type to another. Automatic, or implicit data type conversion happens when: 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.

See Also