Long

This is an abbreviation for Long Integers.
The Long data type can contain whole numbers between -2,147,483,648 and 2,147,486,647
This data type uses 4 bytes
One byte is used to represent the sign (either positive or negative)
The default value is 0.

Dim myLong As Long 

Adding Two Numbers

Declare two variables with the values (5 and 12) and add them together.

Dim myLong1 As Long 
Dim myLong2 As Long
Dim myResult As Long

myLong1 = 5
myLong2 = 12
myResult = myLong1 + myLong2

Debug.Print myResult ' = 17

Integer Rounding <> 0.5

Declare two variables with the values (5.4) and 12.6) and add them together.

Dim myLong1 As Long 
Dim myLong2 As Long
Dim myResult As Long

myLong1 = 5.4 ' rounded down to 5
myLong2 = 12.6 ' rounded up to 13
myResult = myLong1 + myLong2

Debug.Print myResult ' = 18

Integer Rounding - 0.5

Declare two variables with the values (5.5) and 12.5) and add them together.
Any fractional parts equal to 0.5 are rounded (down or up) to the nearest even number.
Although this type of rounding is unusual it is by design to compensate for a bias that could accumulate when adding a lot of 0.5 fractions.

Dim myLong1 As Long 
Dim myLong2 As Long
Dim myResult As Long

myLong1 = 0.5 ' rounded down to 0
myLong2 = 0.5 ' rounded down to 0
myResult = myLong1 + myLong2
Debug.Print myResult ' = 0

myLong1 = 1.5 ' rounded up to 2
myLong2 = 1.5 ' rounded up to 2
myResult = myLong1 + myLong2
Debug.Print myResult ' = 4

myLong1 = 1.5 ' rounded up to 2
myLong2 = 2.5 ' rounded down to 2
myResult = myLong1 + myLong2
Debug.Print myResult ' = 4

Conversion Function

The CLNG function returns an expression converted to a Long data type.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext