Solved

Round question

Posted on 2004-03-23
8
513 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:EYoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Expert Comment

by:cfry001
ID: 10660856
That is normal rounding behaviour. 1.49 will round down, 1.5 up
0
 
LVL 2

Expert Comment

by:cfry001
ID: 10660907
sorry ... to continue ....
on my machine same behaviour
 12
 2
 112

maybe it is a precision problem?
0
 
LVL 2

Expert Comment

by:cfry001
ID: 10660944
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10660948
Round() rounds to the nearest even number. It's normal behavior and is called "Banker's rounding"/"Gaussian rounding" aside from "Standard rounding".
0
 
LVL 9

Expert Comment

by:p_sie
ID: 10660999
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

0
 
LVL 4

Expert Comment

by:learning_t0_pr0gram
ID: 10661165
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
0
 
LVL 19

Accepted Solution

by:
Shauli earned 25 total points
ID: 10662116
FormatNumber(1.5, 0) = 2
FormatNumber(11.5, 0) = 12
FormatNumber(112.5, 0) = 113

S
0
 
LVL 7

Author Comment

by:EYoung
ID: 10662752
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using Access 8 93
How does CurrentUser work? 10 44
How to measure sizes and angles in scanned images ? 3 99
pop out of webbrowser1 control vba6 5 51
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question