Results 1 to 7 of 7

Thread: [RESOLVED] 1 / 3600 = 0???????

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Resolved [RESOLVED] 1 / 3600 = 0???????

    1 / 3600 is not giving me a decimal, but a zero. How do I retrieve the decimal version of the solution?

  2. #2
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,892

    Re: 1 / 3600 = 0???????

    Please show code - what are your variable types?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: 1 / 3600 = 0???????

    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

  4. #4
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: 1 / 3600 = 0???????

    Code:
    Dim theDivide As Single
    theDivide = 1 / 3600
    MsgBox theDivide
    MsgBox Round(theDivide, 5)

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: 1 / 3600 = 0???????

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: 1 / 3600 = 0???????

    Thank you so much!

  7. #7
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    813

    Re: [RESOLVED] 1 / 3600 = 0???????

    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:
    Code:
    ? CInt( "1" ) / CInt( "3600" )
    0
    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:
    ? 1 / 3600#   ' Type Declaration Character 
    ? 1 / CDbl( 3600 )   ' Explicit conversion function - more readable
    Also note that just assigning the result to a "bigger" variable will not do this:
    Code:
    Dim a as Double
    a = 1 / 3600 
    ? a 
    0
    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.

    Regards, Phill W.

Tags for this Thread

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