CLng Function
Returns an expression that has been converted to a Variant of subtype Long.
Syntax
CLng(expression)
Remarks
The expression argument is any valid expression.
Use the CLng
function to provide internationally aware conversions
from any other data type to a Long
subtype. For example, different
decimal separators are properly recognized depending on the locale setting of your
system, as are different thousand separators.
If expression lies outside the acceptable range for the Long
subtype, an error occurs.
The following example uses the CLng
function to convert a value to a
Long
:
Example 1:
Dim MyVal1, MyVal2, MyLong1, MyLong2
MyVal1 = 25427.45: MyVal2 = 25427.55 ' MyVal1, MyVal2 are Doubles.
MyLong1 = CLng(MyVal1)
'Output: 25427
MyLong2 = CLng(MyVal2)
'Output: 25428
Example 2:
Dim MyString, MyLong
MyString = "123456.78"
MyLong = CLng(MyString)
'Output: 123457
Example 3:
Dim MyLocaleString
MyLocaleString = "12.345,67" ' Locale-specific decimal separator.
MyLong = CLng(MyLocaleString) ' Convert to Long, interpreted based on locale.
'MyLong contains the correctly interpreted long value within locale settings
Example 4:
MyDouble = 2.6
MyLong = CLng(MyDouble) ' 2.6 rounds to 3.
'Output: 3
Example 5:
MyDouble = 2.4
MyLong = CLng(MyDouble) ' 2.4 rounds to 2.
'Output: 2
Example 6:
MyDouble = 1.5
MyLong = CLng(MyDouble) ' 1.5 rounds to 2 (nearest even number).
'Output: 2
Example 7:
MyDouble = 0.5
MyLong = CLng(MyDouble) ' 0.5 rounds to 0 (nearest even number).
'Output: 0
Note:
CLng
differs from the Fix and Int functions, which truncate,
rather than round, the fractional part of a number. When the fractional part is
exactly 0.5, the CLng
function always rounds it to the nearest
even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.