Results 1 to 5 of 5

Thread: Problem using greater than or equal with floats and ints

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    72

    Question

    I have a function that I created to handle the >= operations for two different types of numeric values. I am comparing numbers in a Excel worksheet which are actually strings. (does that make sense? Like "2.0" as a string, not a float). So I figured out that when I am passing floats they will always we less than or equal to 1 since the floats in this case are always going to be percentages. The #'s = to 1 or greater I treat as ints and cast them with CInt() and then do >=. This has worked until I got my new set of data which has floats in this form: "4.777777-E2", where this is actually ".04777777". It trys to do a string compare and 4.77777-E2 is greater than any other percentage since in using the string compare it looks at the first digit and compares the first digit of both strings to see which is greater.

    Here is the code... If I knew how to cast a string to a float (if it is possible in VB) then this problem would have been solved a while ago.

    Maybe some of u know what i can do:

    Function greaterThanOrEqual(ByVal Temp1 As String, ByVal temp2 As String) As Boolean
    If ((Temp1 <= 1) Or (temp2 <= 1)) Then ' they are floats.. use > compares
    If (Temp1 >= temp2) Then
    greaterThanOrEqual = True
    Else
    greaterThanOrEqual = False
    End If
    Else
    If (CInt(Temp1) >= CInt(temp2)) Then
    greaterThanOrEqual = True
    Else
    greaterThanOrEqual = False
    End If
    End If



    Thanx in advance,
    cLocKwOrk

  2. #2
    Junior Member
    Join Date
    Nov 2000
    Location
    South Carolina
    Posts
    16

    Could you use?

    If (CDbl(Temp1) >= CDbl(left(temp2, len(temp2)-3)) Then
    greaterThanOrEqual = True
    Else
    greaterThanOrEqual = False
    End If

    in place of the

    If (Temp1 >= temp2) Then
    greaterThanOrEqual = True
    Else
    greaterThanOrEqual = False
    End If


    This should convert the string to a double then compare them

    [Edited by jm3ksc on 11-29-2000 at 12:35 PM]
    Jonathan

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    72

    Red face NM... i figured it out. =P

    Ok I guess u can cast everything to a double and and just do a normal >= compare. Pretty simple.

    -cLocKwOrk

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    72
    Thanks for the help jm3ksc. Funny. I figured it out just as u posted it. =)


  5. #5
    Junior Member
    Join Date
    Nov 2000
    Location
    South Carolina
    Posts
    16

    Wink No Problem

    I know what a pain it can be sometime, was hoping I could finally help someone, since all I ever do is just come here for help...
    Jonathan

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