Results 1 to 6 of 6

Thread: [RESOLVED] Round to nearest specified value

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Resolved [RESOLVED] Round to nearest specified value

    I've been using this code to round off values but there is a problem when I try to round to a decimal value. RoundToNearestVal = 0.1 and a Double or Variant type. It takes the value as 0 so I get a division by 0 error. What am I doing wrong here?

    Code:
    Public Function RoundToNearest(NumberToRound, RoundToNearestVal)
    'Function rounds up or down to the nearest value
        RoundToNearest = RoundToNearestVal * (NumberToRound \ RoundToNearestVal)
        If Abs(NumberToRound - RoundToNearest) > RoundToNearestVal / 2 Then
            If NumberToRound >= 0 Then
                RoundToNearest = RoundToNearest + RoundToNearestVal
            Else
                RoundToNearest = RoundToNearest - RoundToNearestVal
            End If
        End If
    End Function
    Attached Images Attached Images  

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