SIMPLE- Double to Integer (No Rounding)

Discussion in 'AutoCAD' started by spanqy, May 5, 2004.

  1. spanqy

    spanqy Guest

    This has got to be simple: I want result of i = 4 for both cases below:

    Dim i As Integer, d As Double

    d = 4.33
    i = d
    'i = 4 when d = 4.33

    d = 4.87
    i = d
    'i = 5 when d = 4.87

    The implicit cast from Double to Integer (i=d) is apparently rounding up or down. After all these years, I never thought this was normal behavior. So how do I get i to equal 4, for all cases where d >= 4.00 and d <= 4.99?

    Thanks in advance,
     
    spanqy, May 5, 2004
    #1
  2. Use the FIX function !

    --
    Saludos, Ing. Jorge Jimenez, SICAD S.A., Costa Rica
    (sorry, phony e-mail, SPAM made me do it)
    or down. After all these years, I never thought this was normal behavior.
    So how do I get i to equal 4, for all cases where d >= 4.00 and d <= 4.99?
     
    Jorge Jimenez, May 5, 2004
    #2
  3. spanqy

    spanqy Guest

    I got my answer elsewhere but wanted to share; According to my Guru:

    Use Fix function in VB/VBA to get the floor value

    Here is the MS link

    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q196652

    or

    Use these functions to Get the Floor / CeilingValue.

    public function Floor(value as double)

        Floor = CDbl(CLng(value - 0.5))

    end function
    public function Ceiling(value as double)

        Ceiling = CDbl(CLng(value + 0.5))

    end function
     
    spanqy, May 6, 2004
    #3

  4. You could also use Int(value)
     
    C-Services Holland b.v., May 6, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.