
Round function
Hi,
I got a variable Dim as Double containing the value 1.605. I put this double
in the function Round like this Round(dVar,2). The "2" mean that I want this
double to have two decimals. I though that this function should round my
double but it return 1.60 instead of 1.61.
I got VB6 SP5 running on Win95 (don't ask me why...it's for test purpose).
Does anybody knows why it do this ?
Do you know an other function that can do the rounding of my Doubles ?
Thank you,
DaSaint
webmaster@obscuresun.com

Re: Round function
If you just want to truncate the number, you can use the Int function:
dVar = Int(100 * dVar) / 100
"DaSaint" <webmaster@obscuresun.com> wrote in message
news:3c9bab13$1@10.1.10.29...
>
> Hi,
>
> I got a variable Dim as Double containing the value 1.605. I put this
double
> in the function Round like this Round(dVar,2). The "2" mean that I want
this
> double to have two decimals. I though that this function should round my
> double but it return 1.60 instead of 1.61.
>
> I got VB6 SP5 running on Win95 (don't ask me why...it's for test purpose).
>
> Does anybody knows why it do this ?
> Do you know an other function that can do the rounding of my Doubles ?
>
> Thank you,
>
> DaSaint
> webmaster@obscuresun.com

Re: Round function
The vba Round() function uses banker's rounding which rounds to the nearest
even number to try to reduce bias created by always rounding the same
direction.
So 1.605 rounds to the nearest even number (0) and produces 1.60 (down).
1.615 rounds to 1.62 (up).
VB KB article Q196652 has a description of this and other rounding
functions.
Steve.
"DaSaint" <webmaster@obscuresun.com> wrote in message
news:3c9bab13$1@10.1.10.29...
>
> Hi,
>
> I got a variable Dim as Double containing the value 1.605. I put this
double
> in the function Round like this Round(dVar,2). The "2" mean that I want
this
> double to have two decimals. I though that this function should round my
> double but it return 1.60 instead of 1.61.
>
> I got VB6 SP5 running on Win95 (don't ask me why...it's for test purpose).
>
> Does anybody knows why it do this ?
> Do you know an other function that can do the rounding of my Doubles ?
>
> Thank you,
>
> DaSaint
> webmaster@obscuresun.com

Re: Round function
Here is a function to always round up:
Function RoundUp(ByVal NumberToRound As Double, ByVal DecimalPlaces As
Integer) As Double
NumberToRound = (NumberToRound * (10 ^ DecimalPlaces)) + 0.5
NumberToRound = Int(NumberToRound)
RoundUp = NumberToRound / (10 ^ DecimalPlaces)
End Function
Steve.
"DaSaint" <webmaster@obscuresun.com> wrote in message
news:3c9bab13$1@10.1.10.29...
>
> Hi,
>
> I got a variable Dim as Double containing the value 1.605. I put this
double
> in the function Round like this Round(dVar,2). The "2" mean that I want
this
> double to have two decimals. I though that this function should round my
> double but it return 1.60 instead of 1.61.
>
> I got VB6 SP5 running on Win95 (don't ask me why...it's for test purpose).
>
> Does anybody knows why it do this ?
> Do you know an other function that can do the rounding of my Doubles ?
>
> Thank you,
>
> DaSaint
> webmaster@obscuresun.com

Re: Round function
Here is a post I once gave to someone who noted the problem with Round (it also answers
your question at the end). The question dealt with why does Round(2.375, 2) and
Round(2.385, 2) both yield 2.38 as an answer.
Rick
It is not a "bug", it is the way VB was designed to work. It uses something known as
Banker's rounding which, if the number ends in exactly 5 and you want to round to the
position in front of the 5, it rounds numbers down if the number in front of the 5's
position is even and rounds up otherwise. It is supposed to protect against repeated
calculation using rounded numbers so that answer aren't always biased upward.
In any event, I agree with you  it is not what normally would be expected. Also beware
of CInt, Clng and *almost* any other VB function that performs rounding. The one
exception  Format. In place of
Round(YourNumber, Precision)
you can use for the general case
Format$(YourNumber, "0." & String$(Precision, "0"))
which looks somewhat intimidating. But note the simpler format in specific cases. For
example,
Round(YourNumber, 3)
becomes
Format$(YourNumber, "0.000")
You could also use straight math as follows (this always works):
Sgn(Number) * Int(0.5 + Abs(Number) * _
10 ^ Precision) / 10 ^ Precision
which always rounds 5's (or greater) away from zero. That is 123.45 and 123.45 become
123.5 and 123.5 respectively. If you always want to round upward (that is 123.45
becomes 123.4 which is larger than 123.45; 123.45 still becomes 123.5), then use this
instead
Int(0.5 + Number * 10 ^ Precision) / 10 ^ Precision
Although the documentation for it is not as prominent as it should be, you can find out
about Banker's Rounding in the Remarks section of the Type Conversion Functions entry in
VB's help files. (And while it seems to deal only with rounding from one decimal place to
an exact integer, it actually deals with any number whose decimal part ends in 5 which is
being rounded up one decimal place.) Here is the quote from that section:
"When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest
even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. CInt and CLng differ from
the Fix and Int functions, which truncate, rather than round, the fractional part of a
number. Also, Fix and Int always return a value of the same type as is passed in."
Rick
"DaSaint" <webmaster@obscuresun.com> wrote in message news:3c9bab13$1@10.1.10.29...
>
> Hi,
>
> I got a variable Dim as Double containing the value 1.605. I put this double
> in the function Round like this Round(dVar,2). The "2" mean that I want this
> double to have two decimals. I though that this function should round my
> double but it return 1.60 instead of 1.61.
>
> I got VB6 SP5 running on Win95 (don't ask me why...it's for test purpose).
>
> Does anybody knows why it do this ?
> Do you know an other function that can do the rounding of my Doubles ?
>
> Thank you,
>
> DaSaint
> webmaster@obscuresun.com

Re: Round function
Will this help:
Public Const vb_roundup = 1
Public Const vb_rounddown = 0
Function Rnd2Num(Amt As Variant, RoundAmt As Variant, _
Direction As Integer) As Double
On Error Resume Next
If Direction = vb_rounddown Then
Rnd2Num = Int(CDec(Amt) / RoundAmt) * RoundAmt
Else
Rnd2Num = Int(CDec(Amt) / RoundAmt) * RoundAmt
End If
End Function

Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
"DaSaint" <webmaster@obscuresun.com> wrote in message
news:3c9bab13$1@10.1.10.29...
>
> Hi,
>
> I got a variable Dim as Double containing the value 1.605. I put this
double
> in the function Round like this Round(dVar,2). The "2" mean that I want
this
> double to have two decimals. I though that this function should round my
> double but it return 1.60 instead of 1.61.
>
> I got VB6 SP5 running on Win95 (don't ask me why...it's for test purpose).
>
> Does anybody knows why it do this ?
> Do you know an other function that can do the rounding of my Doubles ?
>
> Thank you,
>
> DaSaint
> webmaster@obscuresun.com

Re: Round function
DaSaint,
As Steve mentions this problem is created because MS decided to use banker's
rounding. This has always been a problem with the rounding function in VB. I
complained about this several years ago to MS but they seem to like it as it
is.
By the way, Steve's example is fine, except for one thing. It doesn't take
into account negative numbers. Here's an example that includes negative
numbers:
Function RoundD(Valor#, Optional num_dec) As Double
If IsMissing(num_dec) Then
numdecimals% = DefaultDecimals
Else
numdecimals% = num_dec
End If
If Valor# < 0 Then
valor2# = Valor# * 10 ^ numdecimals%
RoundD = Int(valor2# + 0.5) / 10 ^ numdecimals%
Else
valor2# = Valor# * 10 ^ numdecimals%
RoundD = Int(valor2# + 0.5) / 10 ^ numdecimals%
End If
End Function
Gary
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Development Centers
 Android Development Center
 Cloud Development Project Center
 HTML5 Development Center
 Windows Mobile Development Center
