Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[4252] A better rounding function

Last post 04-23-2007 5:18 AM by NongOil. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [4252] A better rounding function

    This thread is for discussions of A better rounding function.

    • Post Points: 15
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 05-14-2004 10:36 AM In reply to

    • tsutommy
    • Not Ranked
    • Joined on 05-14-2004
    • New Member
    • Points 5

    A better rounding function

    This current function fails for some decimal values.  A -.99 which would round to -1, rounds to 0 with the current code.  Sorry, I can't remember the other issue I had with it but my fixes below took care of both my problems.

       Public Function RoundAdv(ByVal dVal As Double, Optional ByVal iPrecision As Integer = 0) As Double
           Dim roundStr As String
           Dim WholeNumberPart As String
           Dim DecimalPart As String
           Dim i As Integer
           Dim RoundUpValue As Double
           Dim negative As Boolean
           roundStr = CStr(dVal)

               If InStr(1, roundStr, ".") = 0 Then
                   RoundAdv = dVal
                   Exit Function
               End If
               If dVal < 0 Then
                   negative = True
               End If
               WholeNumberPart = Mid$(roundStr, 1, InStr(1, roundStr, ".") - 1)
               DecimalPart = Mid$(roundStr, (InStr(1, roundStr, ".")))
               If Len(DecimalPart) > iPrecision + 1 Then
                   Select Case Mid$(DecimalPart, iPrecision + 2, 1)
                       Case "0", "1", "2", "3", "4"
                           DecimalPart = Mid$(DecimalPart, 1, iPrecision + 1)
                       Case "5", "6", "7", "8", "9"
                           RoundUpValue = 0.1
                           For i = 1 To iPrecision - 1
                               RoundUpValue = RoundUpValue * 0.1
                           Next
                           DecimalPart = CStr(Val(Mid$(DecimalPart, 1, iPrecision + 1)) + RoundUpValue)
                           If Mid$(DecimalPart, 1, 1) <> "1" Then
                               DecimalPart = Mid$(DecimalPart, 2)
                           Else
                               If negative Then
                                   WholeNumberPart = CStr(Val(WholeNumberPart) - 1)
                               Else
                                   WholeNumberPart = CStr(Val(WholeNumberPart) + 1)
                               End If
                               DecimalPart = ""
                           End If
                   End Select
               End If
               RoundAdv = Val(WholeNumberPart & DecimalPart)
               If RoundAdv <> 0 Then
                   If negative And RoundAdv > 0 Then
                       RoundAdv = -1 * RoundAdv
                   End If
               End If
    • Post Points: 0
  • 04-23-2007 5:18 AM In reply to

    • NongOil
    • Not Ranked
    • Joined on 03-30-2007
    • Thailand
    • New Member
    • Points 5

    Re: [4252] A better rounding function

    Please try this

    Public Function RoundAdv(ByVal Number As Variant, Optional lDigit As Long = 0) As Double
        
       If Not IsNumeric(Number) Then Exit Function
       RoundAdv = Round(CDbl(Number) + (IIf(Number < 0, -1, 1) / (10 * (10 ^ lDigit))), lDigit)


    End Function

    • Post Points: 5
Page 1 of 1 (3 items)