rounding in access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziccardi
    New Member
    • Mar 2010
    • 12

    rounding in access

    I have a number 6.5149598935246 4 that I need to round to 6.51

    I am using the following code:
    Public Function MyRound(dblInpu t As Currency, intDecimal As Integer) As Double

    If dblInput < 0 Then
    MyRound = -Int(CDec((Abs(d blInput) * 10 ^ intDecimal) + 0.5)) / 10 ^ intDecimal
    Else
    MyRound = Int(CDec((dblIn put * 10 ^ intDecimal) + 0.5)) / 10 ^ intDecimal
    End If


    End Function

    When I step into the code what is actually going through the MyRound code is 6.515 so it makes sense that it is rounding to 6.52 but I do not know why it is rounding from 6.5149598935246 4 to 6.515.

    It's probably very simple but I'm not seeing it.
  • ziccardi
    New Member
    • Mar 2010
    • 12

    #2
    one other wierd quirk: for some reason the code is multiplying
    ( 210.16 ) * ( 3.0999999493360 5E-02 )
    it should be multiplying
    ( 210.16 ) * ( 0.0310)

    I know it's not much of a difference but the 0.0310 is being pulled directly from a table where the field is defined as Number-Single-Fixed (4 decimal places)

    Thanks!

    Comment

    • gershwyn
      New Member
      • Feb 2010
      • 122

      #3
      Have you tried using the built in round function?

      Code:
      Round(6.51495989352464, 2)
      =6.51

      Comment

      • ziccardi
        New Member
        • Mar 2010
        • 12

        #4
        The built in round function in Access uses Banker' Rounding logic. Banker's rounding rounds .5 up sometimes and down sometimes.

        I need it to be Symmetric arithmetic rounding where it rounds .5 away from 0. Similar to Excel Worksheet Round function.

        Besides it seems to be rounding correctly but it looks like the number is being rounded before it even gets to my rounding code. I am going to try changing dblInput to Double in the code and see if that corrects it.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          The result that I am getting is 6.52, namely:
          Code:
          Public Function MyRound(dblInput As Currency, intDecimal As Integer) As Double
            If dblInput < 0 Then
              MyRound = -Int(CDec(Abs(dblInput) * (10 ^ intDecimal)) + 0.5) / (10 ^ intDecimal)
            Else
              MyRound = Int(CDec(dblInput * (10 ^ intDecimal)) + 0.5) / (10 ^ intDecimal)
            End If
          End Function
          Code:
          Debug.Print MyRound(6.51495989352464 ,2)
          OUTPUT:
          Code:
          6.52

          Comment

          • ziccardi
            New Member
            • Mar 2010
            • 12

            #6
            I changed dblInput As Currency to dblInput As Double ... it seems to work now

            Comment

            Working...