Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The mod operator 2

Status
Not open for further replies.

yyysman

Technical User
Jun 22, 2004
1
0
0
US
I am trying to use the mod operator to return a decimal value. However, when I enter 12.6 mod 5 into my VBA code, the result is 3. I would expect the answer to be 2.6. I think that VB is forcing the result to be in the integer form. I have tried forcing the mod to output a decimal by using the following code: CDbl(12.6 mod 5). This code also output 3.

Any help would be greatly appreciated!

P.S. I am using the Option Explit mode.
 
From the help

The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result.

You may want to try

A = 12.6 - int(12.6/5)*5
 
Hi,

Check help to understand how MOD returns values. Consider your own MOD Function
Code:
Function MyMod(a, b)
   MyMod = (x / b - Int(x / b)) * b
End Function


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top