Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with vb's round function

Posted on 2002-04-30
6
Medium Priority
?
220 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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

670 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