Solved

# problem with vb's round function

Posted on 2002-04-30
206 Views
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
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

LVL 143

Expert Comment

ID: 6980594
i have vb6 sp3:

? round(96.645,2)
96.64

Cheers
0

LVL 6

Author Comment

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

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

LVL 143

Expert Comment

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

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

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

Question has a verified solution.

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

### Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
###### Suggested Courses
Course of the Month1 day, 16 hours left to enroll

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

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