Link to home
Start Free TrialLog in
Avatar of EYoung
EYoungFlag for United States of America

asked on

Round question

In vb6 I am having a Rounding problem.

Round(1.5,0) = 2            Makes sense
Round(11.5,0) = 12        Makes sense
Round(112.5,0) = 112    Does not make sense.  Why not 113?

I have defined my memory varialbles as Double, but the rounding as shown above is not working correctly.

Thanks
Avatar of cfry001
cfry001

That is normal rounding behaviour. 1.49 will round down, 1.5 up
sorry ... to continue ....
on my machine same behaviour
 12
 2
 112

maybe it is a precision problem?
found a page
 
http://www.raritanval.edu/departments/cis/full-time/Schwarz/vb6/lesson7.htm

that says that VB6 rounds odd numbers up and even numbers down at .5
Round() rounds to the nearest even number. It's normal behavior and is called "Banker's rounding"/"Gaussian rounding" aside from "Standard rounding".
cfry001 has not read the question!!

Eyoung, check this website, in it is explained by microsoft

http://support.microsoft.com/default.aspx?scid=kb;EN-US;196652

Private Sub Command1_Click()
Dim MyNumber As Double
Dim Tmp As Double
MyNumber = Text1.Text
If Not InStr(1, MyNumber, ".") Then MyNumber = MyNumber + ".1"
If Mid(MyNumber, 1, InStr(1, MyNumber, ".") - 1) Mod 2 = 0 Then
If Mid(MyNumber, InStr(1, MyNumber, ".") + 1) <> 5 Then
MyNumber = MyNumber + 0.1
End If
End If
MsgBox Round(MyNumber, 0)
End Sub

that will fix the problem, if u don't mind a little code
ASKER CERTIFIED SOLUTION
Avatar of Shauli
Shauli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EYoung

ASKER

Thanks to everyone.  cfry001 - you were right.  VB6 does have an error that was not present in vb5.  Also, MS is not consistent among its apps in how it calcs rounding.  I read that same url.

Shauli - you provided the answer that was the most helpful, thanks.