Solved

problem with vb's round function

Posted on 2002-04-30
6
190 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 142

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 142

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now