Truncate a double

Discussion in 'AutoCAD' started by Matthew.Corbin, May 19, 2004.

  1. Hello all,
    Looking for an answer to this question. I've looked through the NG and couldn't find a problem quite like what i'm experiencing. I would like to truncate a number. The only way I describe it is as follows:

    1125.32566 truncated to 25.32 or 965.32566 truncated to 65.32. I've gotten the rounding part down, but I can't seem to find a function that will cut off my number to the left of the decimal. Any solutions?
    Thanks for your time.
    Matthew Corbin
     
    Matthew.Corbin, May 19, 2004
    #1
  2. Matthew.Corbin

    Jackrabbit Guest

    [pre]
    Public Function FormatNumber(InputNumber As Double) As String
    Dim Index As Integer
    Dim OutputString As String
    OutputString = Format(InputNumber, "#.00")
    Index = InStr(1, OutputString, ".")
    If Index > 3 Then
    OutputString = Mid$(OutputString, Index - 2)
    End If
    FormatNumber = OutputString
    End Function

    Public Sub TestFormatNumber()
    MsgBox FormatNumber(1125.32566)
    MsgBox FormatNumber(965.32566)
    End Sub
    [/pre]
     
    Jackrabbit, May 19, 2004
    #2
  3. Works a treat, thank you for your help.
    Kind Regards,
    Matthew Corbin
     
    Matthew.Corbin, May 19, 2004
    #3
  4. Matthew.Corbin

    David Urban Guest

    Matthew

    I do it a different way. I use the fix command

    dim number as double, fixednum as double
    number = 1125.32566
    fixednumber = fix(number /100)*100 ' this will return 1100
    number = round(number - fixednumber) 'will return 25.32

    David Urban
     
    David Urban, May 19, 2004
    #4
  5. but I can't seem to find a function that will cut off my number to the
    left of the decimal. Any solutions?

    I see you've already got your solutions, but I just thought I'd mention the
    Mod (remainder) function. In your case, "965.32 Mod 100" would return the
    65.32. The rounding or truncating part would still be up to you.
     
    James Belshan, May 20, 2004
    #5
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.