Consulting

Results 1 to 4 of 4

Thread: Truncate and Rounding

  1. #1

    Truncate and Rounding

    The following creates rounding error = don't want rounding - want truncation.

    [VBA]Function Azmth2Brng(lBearing As Single) As String
    lBearing = lBearing - (Int(lBearing / 360) * 360)
    Debug.Print lBearing
    End Function
    Sub testy()
    Azmth2Brng (44465.32)
    End Sub[/VBA]

    I get 185.3203 WHY? Should it not be 185.32?

    Where does .0003 come from and how do I eliminate it?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Function Azmth2Brng(lBearing As Single) As Double
    lBearing = Round(lBearing - ((lBearing \ 360) * 360), 2)
    Debug.Print lBearing
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Single vs Double? Since the object is only a number to the hundreth decimal place, why should I have to use a double to get an accurate answer?

    The string portion is for the final output of the result. The object is to bring in a decimal azmuth and convert it to a bearing: 10.345 degrees = N 10d20'42'' E

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It was the rounding that gave the accuracy, not the double. I just never work in Single or Integer.

    Even if it is to be converted to a bearing, I would get the function to return a number and append the text outwith the function.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •