Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Data > Integers

Integers

& Integer Divides

There are 3 distinct methods for Integer Divides in VBScript: \ y, Int( x / y ) and CInt( x / y )
The best way to explain the differences between the 3 available Integer Divide methods may be by showing some examples.
The following table lists the outcomes of the 3 methods for a range of values:

 

Demonstration of 3 integer divide methods
Abs(x\y) <=
Abs(x/y)
Int(x/y) <= x/y CInt(x/y) = x/y rounded
to nearest integer
-7\3=-2 Int(-7/3)=-3 CInt(-7/3)=-2
-6\3=-2 Int(-6/3)=-2 CInt(-6/3)=-2
-5\3=-1 Int(-5/3)=-2 CInt(-5/3)=-2
-4\3=-1 Int(-4/3)=-2 CInt(-4/3)=-1
-3\3=-1 Int(-3/3)=-1 CInt(-3/3)=-1
-2\3= 0 Int(-2/3)=-1 CInt(-2/3)=-1
-1\3= 0 Int(-1/3)=-1 CInt(-1/3)= 0
 0\3= 0 Int( 0/3)= 0 CInt( 0/3)= 0
 1\3= 0 Int( 1/3)= 0 CInt( 1/3)= 0
 2\3= 0 Int( 2/3)= 0 CInt( 2/3)= 1
 3\3= 1 Int( 3/3)= 1 CInt( 3/3)= 1
 4\3= 1 Int( 4/3)= 1 CInt( 4/3)= 1
 5\3= 1 Int( 5/3)= 1 CInt( 5/3)= 2
 6\3= 2 Int( 6/3)= 2 CInt( 6/3)= 2
 7\3= 2 Int( 7/3)= 2 CInt( 7/3)= 2

 


page last uploaded: 2016-09-19, 14:58