Results 1 to 21 of 21

Thread: select case with non integer numbers

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: select case with non integer numbers

    this is interesting
    the following structure works

    Code:
      powstring = power
    
    Select Case powstring
        Case "0.001"
    'statements
    
    case 1
    'statements
    
    case "60.001"
    'statements
    
    end select

    but not this code

    Code:
      powstring = power
    
    Select Case powstring
    Case 0.001
    'statements
    
    case 1
    'statements
    
    case 60.001
    'statements
    
    end select

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: select case with non integer numbers

    Quote Originally Posted by vb_student
    this is interesting
    the following structure works

    Code:
      powstring = power
    
    Select Case powstring
        Case "0.001"
    'statements
    
    case 1
    'statements
    
    case "60.001"
    'statements
    
    end select

    but not this code

    Code:
      powstring = power
    
    Select Case powstring
    Case 0.001
    'statements
    
    case 1
    'statements
    
    case 60.001
    'statements
    
    end select
    Floating point (single and double) are evil.

    You just fell right into the reason for it.

    DISPLAY a DOUBLE value and you see one thing - compare it in an IF or SELECT/CASE and it all of a sudden doesn't work.

    Something that might display at 4.5 is really stored as 4.499999999999999999

    That's the way DOUBLE works.

    Why can't you use CURRENCY? It doesn't have this problem and the SELECT/CASE will all of a suddent work beautifully.

    If you need double, change the CASE's to a RANGE - not an exact value - a bit below and a bit above the value you are trying to CASE on.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: select case with non integer numbers

    hey in another part of my code, power with 0.001 works OK

    is this some sort of idiosynchracy in VB?

    i had nto decalred the power in that module too. is it because of that, in the module where it was not working i had dxeclared it as a double.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: select case with non integer numbers

    You must understand that DOUBLE will give you these inconsistent results.

    Do you need to store your values as DOUBLE?

    Why doesn't the CURRENCY datatype work for you - it precise and doesn't have the issues that DOUBLE does.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: select case with non integer numbers

    it somehow works now with double, i will leave it at that and if the big guy wants me to clean up the code, i will do it later

    what is the difference between double and currency?

    currency sounds something to do with dollars, pounds and yen not fractional numbers.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: select case with non integer numbers

    Currency is a datatype that allows four digits after the decimal point.

    It's actually a long, with an "implied" decimal - so it has the accuracy of long but the fake decimal point works for you...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: select case with non integer numbers

    thanks for telling me about currencies

    i just tried longs elsewhere in my code and they did not work for fractions

    there does not seem to be a correspondence between variable types in c and VB

    doubles and longs used to accept fractional parts in c but they dont in VB, correct?

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