[RESOLVED] [2005] Get value range of variable
Hi all,
I am having a problem. I need to know how to check if a value is equal to plus or minus five million of another value.
For instance
Code:
if amount 1 = amount2 +- 5000000 then
I know this is not correct, because it assumes you want to subtract 5milli from amount2. I don't actually want to subtract anything from amount2, just see if amount1 falls within that range. For example, if amount1 is 10milli, and amount2 is 13milli, then that is considered a "match" (for the purposes of this question anyway).
I know there's probably something (again) fundamental that I'm missing.
Re: [2005] Get value range of variable
vb Code:
if amount2 > amount1 and amount2 - amount1 <= 5000000 or amount1 > amount2 and amount1 - amount2 <= 5000000 then
' amount1 is equal to plus or minus five million of amount2
end if
Re: [2005] Get value range of variable
Code:
if Amount1 >= (amount2 - 5000000 ) orelse Amount1 <= (amount2 + 5000000 ) then
'Match
end if
eidt: Paul's logic is much better :D
Re: [2005] Get value range of variable
Or if you dont want to type that long line of code over and over again, you can put it in a little function that returns true or false:
Code:
Public Function PlusMinus(ByVal origValue As Integer, ByVal compareValue As Integer)
Dim maxDifference As Integer = 5000000 'In this case, it is 5 million
If compareValue > origValue And compareValue - origValue <= maxDifference _
Or origValue > compareValue And origValue - compareValue <= maxDifference Then
Return True
Else
Return False
End If
End Function
Re: [2005] Get value range of variable
Code:
Dim num1 As Long = 13000000
Dim num2 As Long = 18000000
Dim num3 As Long = Math.Abs(num1 - num2)
Const variance As Long = 5000000
Debug.WriteLine(num3.ToString)
If num3 <= variance Then
Debug.WriteLine("in range")
Else
Debug.WriteLine("outside range")
End If