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
Solved

problem with vb's round function

Posted on 2002-04-30
6
200 Views
Last Modified: 2010-05-02
Hello experts !

Recently i detected a bug in the round function. In the immediate window of the vb-ide i get the following response:

? round(96.645,2)
 96,64

The return value should be 96.65 by definition. Format works properly:

? format(96.645,"0.00")
96,65

Can i get a fix (i have vb6 SP5)or is there an other elegant function. I don't like using format numbers which aren't been displayed or printed.

Also i know that i can use a function like this for example:

Public Function RoundIt(Number As Variant, NumDigitsAfterDecimal As Long) As Variant
    If Not IsNumeric(Number) Then
        RoundIt = Number
    Else
        RoundIt = Int(0.5 + Number * (10 ^ NumDigitsAfterDecimal)) / (10 ^ NumDigitsAfterDecimal)
    End If
End Function

... but this is perhaps a slow and inelegant way like format. Any suggestions ?

Regards

V.K.
0
Comment
Question by:VK
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6980594
i have vb6 sp3:

? round(96.645,2)
 96.64

Cheers
0
 
LVL 6

Author Comment

by:VK
ID: 6980626
Hello angel !

...

? round(96.645,2)
96.64

This is not correct as said before. if you have to round with 2 digits after decimal point you have to evaluate the 3rd digit after decimal:

>=5: round up
<5:  round down

Hope now the problem is clear.

V.K.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 6980634
This is the way that the round function works. There are a whole host of questions here and answers in MSDN which explain why. You have the two best options available: format or a custom function so I would suggest you choose one and live with it. Anyway here is the reason why:

http://support.microsoft.com/support/kb/articles/Q196/6/52.ASP

And

http://support.microsoft.com/support/kb/articles/Q194/9/83.ASP 
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6980650
Sorry, I wanted to post more saying that it won't be the sp that fixes it. Like TimCottee indicates, that's the way round function works...
CHeers
0
 
LVL 6

Author Comment

by:VK
ID: 6980682
Hello Experts !

Thanks to tim (although i have to live with this fact :-)).
I have to do some benchmarking to find out a fast and bulletproof function.

V.K.
0
 
LVL 6

Author Comment

by:VK
ID: 6980685
Hello Experts !

Thanks to tim (although i have to live with this fact :-)).
I have to do some benchmarking to find out a fast and bulletproof function.

V.K.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

829 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