Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Get value range of variable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Location
    Between Try & Catch
    Posts
    249

    Resolved [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.
    If my post helped you, please rate it!

    Languages: VB/ASP.NET 2005, C# 2008,VB6
    Databases: Oracle (knowledge not currently in use), DB2

    FROM Customers
    WHERE We_Know_What_We_Want <> DB.Null
    SELECT *
    0 rows returned

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2005] Get value range of variable

    vb Code:
    1. if amount2 > amount1 and amount2 - amount1 <= 5000000 or amount1 > amount2 and amount1 - amount2 <= 5000000 then
    2.       ' amount1 is equal to plus or minus five million of amount2
    3. end if

  3. #3
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    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
    __________________
    Rate the posts that helped you

  4. #4
    New Member
    Join Date
    Aug 2004
    Location
    Merelbeke, Belgium
    Posts
    12

    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

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width