Rounding in Equations

Greetings,

Since VB6's round() function utilizes banker's rounding,doesanyone know a function that will actually round up/down basedonthe last digit?

SteveSolidworksApi macros
Comments 3
SF
SF 2007-05-22
Banker's rounding?

It does round based on last digit,,,, if you want,,,, numberofplaces is an optional arguement......

Can you be more specific? I may still have a roundfunctionaround here somewhere that I wrote for VBA version 5, butI've hadno use for it for a while.
SF
SF 2007-05-23
I believe the Format function rounds up.

Regards,

Regg
SF
SF 2007-05-23
I'm not sure if you got the answer you wanted, so here are acoupleof functions. the first rounds on last digit regardless ofthenumber of digits. The second lets you specify places. It's notthecleanest, but you can modify to your needs. No errorcontrolincluded.

Option Explicit

 

Function Rnd(Number As Double) As Double

Dim MyNum As String

Dim Digits, StepDigit

MyNum = CStr(Number)

Digits = Len(MyNum)

StepDigit = Mid$(MyNum, Digits - 1, 1)

If Right(MyNum, 1) > 4 Then

    MyNum = Mid$(MyNum,1,Digits - 2)

    StepDigit = StepDigit+1

    MyNum = MyNum&StepDigit

    Rnd = CDbl(MyNum)

Else

    MyNum = Mid$(MyNum,1,Digits - 1)

    MyNum = MyNum

    Rnd = CDbl(MyNum)

End If

End Function

 

Function RndPlaces(Number As Double,OptionalPlaces As Integer) As Double

Dim MyNum As String

Dim Digits, StepDigit

Dim I As Integer, NoDecimal As Boolean

NoDecimal = True

MyNum = CStr(Number)

For I = 1 To Len(MyNum)

    If Mid$(MyNum, I, 1) ="."Then

       Digits = I + Places

       NoDecimal = False

       ExitFor

    End If

Next I

If NoDecimal Then Digits = Places

If Places = 0 Then Digits = Len(MyNum) - 1

StepDigit = Mid$(MyNum, Digits, 1)

If Len(MyNum) > Digits Then

    If Mid$(MyNum, Digits +1,1) > 4 Then

       StepDigit = StepDigit +1

       MyNum = Left(MyNum,Digits - 1)

       MyNum = MyNum &StepDigit

       RndPlaces =CDbl(MyNum)

    Else

       MyNum = Left(MyNum,Digits)

       RndPlaces =CDbl(MyNum)

    End If

Else

    MsgBox "There are NotEnoughDigits in the Number to Round to that Place.",vbCritical, "NotEnough Digits"

End If

EndFunction