VB : CLng Function

Posted by Fluffy | 7:50 PM | , , | 0 comments »

Using CLng function in VB


CLng is a function that is used to convert data into Long data type. It is basically used to round figures with multiple decimal points. See the example shown below.


Dim myData1, myData2 As Double '---Variable declaration
Dim Output1, Output2 As Long

myData1 = 5469.61 '---Initialization of data
myData1 = 5469.48

Output1 = CLng(myData1) '--- convert data into Long data type
Output2 = CLng(myData2)

MsgBox(Output1) '---Output is 5470
MsgBox(Output2) '---Output is 5469



However, there is also one interesting thing to note that this CLng function rounds figures to the nearest even number, that is (0,2,4,6,8) if the fraction part of a multi-decimal point figure is exactly 0.5. See the second example below to properly illustrate this special feature.



Dim myData1, myData2 As Double '---Variable declaration
Dim Output1, Output2 As Long

myData1 = 5464.5 '---Initialization of data
myData1 = 5475.5

Output1 = CLng(myData1) '--- convert data into Long data type
Output2 = CLng(myData2)

MsgBox(Output1) '---Output is 5464
MsgBox(Output2) '---Output is 5476



Note that conversion of 5464.5 in Output1 becomes 5464 instead of 5465 because 5464 is the nearest even number whereas 5465 is an odd number. The same applies to Output2 as well. Therefore, that illustrates the concept of applying CLng function into your VB programming code.

See Also CType Function

0 comments