1 / 3600 is not giving me a decimal, but a zero. How do I retrieve the decimal version of the solution?
Printable View
1 / 3600 is not giving me a decimal, but a zero. How do I retrieve the decimal version of the solution?
Please show code - what are your variable types?
Code:Dim theMin As Long, theMax As Long, thePos As Long, theTop As Long, theBottom As Long, theDivide As Long
theMin = 2400: theMax = 6000: thePos = 2401
theTop = thePos - theMin
theBottom = theMax - theMin
theDivide = theTop / theBottom
MsgBox theDivide * 100
Code:Dim theDivide As Single
theDivide = 1 / 3600
MsgBox theDivide
MsgBox Round(theDivide, 5)
Long can not hold a decimal, is for whole numbers only hence the result of 0 from the code in post #3
You must use either single, double or currency if it is to hold a decimal value
Thank you so much!
You've fallen foul of VB's "helpful" attempt to be run-time efficient.
If it can "get away" with Integer arithmetic, then it will.
1 is an Integer literal.
3600 is an Integer literal.
So VB tries to work out:
You need to tell VB that you want a little more precision in your calculation, by introducing a "bigger" data type into the expression:Code:? CInt( "1" ) / CInt( "3600" )
0
Also note that just assigning the result to a "bigger" variable will not do this:Code:? 1 / 3600# ' Type Declaration Character
? 1 / CDbl( 3600 ) ' Explicit conversion function - more readable
This is because the expression is evaluated first and then the result of that expression assigned to the target variable, as a completely separate operation.Code:Dim a as Double
a = 1 / 3600
? a
0
Regards, Phill W.