Solved

problem with vb's round function

Posted on 2002-04-30
6
211 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
[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
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

632 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