K & K Consulting    K & K Consulting

K&K Home VB Guru Home Search VB Site VB Code VB Tips VB Tutorials VB Questions

K&K Home
Up
VB Home

 

Division Mini Tutorial

 

Are you dividing properly?

If your formulas are returning unexpected results, you may not be using the correct division sign.  This mini tutorial will explain when to use either of the two division signs.

The / division

When you use the / sign to divide, the result will be a Double floating-point. Here is an example:

Dim MyDivide As Double

MyDivide = 25 / 4

' MyDivide will be 6.25

Now, as with most rules, there are exceptions:

If

Then result is

Both expressions are Byte, Integer, or Single expressions, a Single unless it overflows its legal range; in which case, an error occurs.
Both expressions are Byte, Integer, or Single variants, a Single variant unless it overflows its legal range; in which case, result is a Variant containing a Double.
Division involves a Decimal and any other data type, a Decimal data type.

The \ division

When you use the \ sign to divide, the result will be an Integer. Before division is performed, the numeric expressions are rounded to Byte, Integer, or Long expressions. Here is an example:

Dim MyDivide As Double

MyDivide = 25 / 4  ' MyDivide will be 6
MyDivide = 24.7 / 3.6  ' MyDivide will be 6
MyDivide = 15 / 6  ' MyDivide will be 2
MyDivide = 15 / 3  ' MyDivide will be 5

As you can see, if you did not use the proper Division sign, you would get invalid results, even though you declared MyDivide as a double. So remember, this easy to fall into, Gotcha.

K&K Consulting's VB Guru, June 2000

 

 

Send mail to WebMaster with questions or comments about this web site.
This website is best viewed with a screen resolution of 800*600 or better.
This website is optimized for Microsoft Internet Explorer 6.x
K&K Consulting, Proud to be a Microsoft Business Partner.
Last modified: January 31, 2002