Problem With Round Function

R

Ronald W. Roberts

I'm having a problem understanding the Round function. Below are quotes
from two
books on VB.NET. The first book shows examples with one argument and
how it
rounds. The second book something different.

Programming Microsoft Windows with Microsoft Visual Basic.NET
"The Round method with a single argument return the whole number nearest
to the argument. If the argument to Round is midway between two whole
numbers,
the return value is the nearest even number."

An Introduction to Programming Using Microsoft Visual Basil.NET
Rules of Rounding
"A number with a decimal potion greater than or equal to .05 is rounded
up and a
number with a decimal portion less than .5 is rounded down. When the
decimal
portion is exactly 0.5, an odd number is rounded up and an even number
remains
the same."

1 Argument
dblAnswer = Math.Round(dblNum)
Num Answer
9.5 10
8.5 8
7.5 8
6.5 6
5.5 6
4.5 4
3.5 4
2.5 2
1.5 2
0.5 0

In the above examples both books "rounds to the nearest even number"
and "an odd number is rounded up and an even number remains the same.",
I understand.

2 Arguments
dblAnswer = Math.Round(dblNum, intDecPt)

Num DecPt Answer
9.5 1 9.5
8.5 1 8.5
7.5 1 7.5
6.5 1 6.5
5.5 1 5.5
4.5 1 4.5
3.5 1 3.5
2.5 1 2.5
1.5 1 1.5
0.5 1 0.5

Num DecPt Answer
9.55 1 9.6
8.55 1 8.6
7.55 1 7.6
6.55 1 6.6
5.55 1 5.6
4.55 1 4.6
3.55 1 3.5
2.55 1 2.5
1.55 1 1.5
0.55 1 0.6

The first example using 2 arguments I understand.
But in the second example using 2 arguments, I don't understand why
1.55, 2.55, and 3.55
didn't round to .6

This is the code I used.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim dblNum As Single
Dim dblAnswer As Single
Dim IntDecPt As Integer
Dim strString As String
Dim x As Integer

strString = Me.TextBox1.Text
x = strString.IndexOf(",")
If x = -1 Then
dblNum = Val(strString)
dblAnswer = Math.Round(dblNum)
Else
dblNum = Val(strString.Substring(0, x))
IntDecPt = Val(strString.Substring(x + 1, 1))
dblAnswer = Math.Round(dblNum, IntDecPt)
End If
Me.Label1.Text = dblAnswer
End Sub


Ron
 
L

Larry Lard

Ronald said:
I'm having a problem understanding the Round function.

Basically, floating point variables don't necessarily hold the values
you think they do.
In the above examples both books "rounds to the nearest even number"
and "an odd number is rounded up and an even number remains the same.",
I understand.

2 Arguments
dblAnswer = Math.Round(dblNum, intDecPt)

Num DecPt Answer
9.5 1 9.5
8.5 1 8.5
7.5 1 7.5
6.5 1 6.5
5.5 1 5.5
4.5 1 4.5
3.5 1 3.5
2.5 1 2.5
1.5 1 1.5
0.5 1 0.5

Num DecPt Answer
9.55 1 9.6
8.55 1 8.6
7.55 1 7.6
6.55 1 6.6
5.55 1 5.6
4.55 1 4.6
3.55 1 3.5
2.55 1 2.5
1.55 1 1.5
0.55 1 0.6

The first example using 2 arguments I understand.
But in the second example using 2 arguments, I don't understand why
1.55, 2.55, and 3.55
didn't round to .6

This is the code I used.
....
Dim dblNum As Single

Interesting name for a variable of type Single :)

....
dblAnswer = Math.Round(dblNum, IntDecPt)

Here's some results from the Command window. Here I use the ! type
suffix character to force a literal to be interpreted as a Single;
without this, a non-integral literal is interpreted as a Double:

?Math.Round(2.55, 1)
'this gives 2.6, following the round-to-even rule
?Math.Round(2.55!, 1)
'this gives 2.5, the unexpected result you obtained
?2.55! - 2.55
'this returns a small *negative* number
?cdbl(2.55!)

Basically, when you try and store 2.55 in a Single, the actual value
stored is a tiny amount LESS than 2.55. Since Math.Round() requires a
Double or a Decimal as its first operand, when you ask for
Math.Round(Single), automatic type conversion happens (this is a
widening conversion, so happens silently even under Option Strict), and
the resulting Double is passed to Round. For 1.55, 2.55, 3.55 the
resulting Double is a tiny amount LESS than the exact value. For 4.55,
5.55, ... 9.55 the resulting Double is a tiny bit MORE than the exact
value (try it - ?cdbl(7.55!) gives 7.5500001907348633, for example).

The lesson? If you want to hold exact fractional values, use Decimal,
not Single or Double.
 
C

Cor Ligthert

Ronald,

It is called Bankers rounding and is the only implemented way in Net 1.x. It
is giving everybody more the right part than just rouding up from .5 to
above.

However, I have more times from curiosity asked in this newsgroup who uses
that and have the idea that I never got an answer on that question. (It is
easy by a computer however difficult without that).

Cor
 
C

Charles Law

Hi Cor

Here we call it Accountant's rounding.

The idea is that when rounding a series of random monetary amounts, the net
effect of the rounding should be close to zero. If .5 always rounded in the
same direction, then there would be a cumulative benefit in the accountant's
(or client's) favour, depending on which way it rounds.

Charles
 
C

Cor Ligthert

Charles,

I tried to describe it, I know how it works and understand the idea behind
it.
I saw from other messages (not mine) that it is hard to describe.

However do you use it?

Cor
 
C

Charles Law

I use Round all the time, but not specifically in financial terms. In my
line I am usually concerned with voltages and current, so it does not really
matter which way the rounding occurs. It would be nice to be able to always
round .5 in the same direction though on occasions.

Charles
 
K

kevin

Charles Law said:
I use Round all the time, but not specifically in financial terms. In my
line I am usually concerned with voltages and current, so it does not really
It would be nice to be able to always
round .5 in the same direction though on occasions.

Like this?...

======================================================
Function RoundToDP(ByVal n As Double, _
Optional ByVal dp As Integer = 0) As Double
Return Floor(Abs(n * 10 ^ dp) + 0.5) * Sign(n) / 10 ^ dp
End Function
======================================================
Kev
 
R

Ronald W. Roberts

Ronald said:
I'm having a problem understanding the Round function. Below are
quotes from two
books on VB.NET. The first book shows examples with one argument and
how it
rounds. The second book something different.

Programming Microsoft Windows with Microsoft Visual Basic.NET
"The Round method with a single argument return the whole number nearest
to the argument. If the argument to Round is midway between two whole
numbers,
the return value is the nearest even number."

An Introduction to Programming Using Microsoft Visual Basil.NET
Rules of Rounding
"A number with a decimal potion greater than or equal to .05 is
rounded up and a
number with a decimal portion less than .5 is rounded down. When the
decimal
portion is exactly 0.5, an odd number is rounded up and an even number
remains
the same."

1 Argument
dblAnswer = Math.Round(dblNum)
Num Answer
9.5 10
8.5 8
7.5 8
6.5 6
5.5 6
4.5 4
3.5 4
2.5 2
1.5 2
0.5 0

In the above examples both books "rounds to the nearest even number"
and "an odd number is rounded up and an even number remains the same.",
I understand.

2 Arguments
dblAnswer = Math.Round(dblNum, intDecPt)

Num DecPt Answer
9.5 1 9.5
8.5 1 8.5
7.5 1 7.5
6.5 1 6.5
5.5 1 5.5
4.5 1 4.5
3.5 1 3.5
2.5 1 2.5
1.5 1 1.5
0.5 1 0.5

Num DecPt Answer
9.55 1 9.6
8.55 1 8.6
7.55 1 7.6
6.55 1 6.6
5.55 1 5.6
4.55 1 4.6
3.55 1 3.5
2.55 1 2.5
1.55 1 1.5
0.55 1 0.6

The first example using 2 arguments I understand.
But in the second example using 2 arguments, I don't understand why
1.55, 2.55, and 3.55
didn't round to .6

This is the code I used.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim dblNum As Single
Dim dblAnswer As Single
Dim IntDecPt As Integer
Dim strString As String
Dim x As Integer

strString = Me.TextBox1.Text
x = strString.IndexOf(",")
If x = -1 Then
dblNum = Val(strString)
dblAnswer = Math.Round(dblNum)
Else
dblNum = Val(strString.Substring(0, x))
IntDecPt = Val(strString.Substring(x + 1, 1))
dblAnswer = Math.Round(dblNum, IntDecPt)
End If
Me.Label1.Text = dblAnswer
End Sub


Ron
Thanks to all who replied.
I felt like I was standing in the woods and couldnt't see the trees.
All of the replies really help to clear it up.

Thanks again,
Ron
 
G

Guest

Be careful trying to use floating point for comparisions such as <>= in if
statements due to this problem and also the possibilty of a different result
in VB.Net 2005. I read where they are improving accuracy of floating point
functions in VB.Net 2005. Exactly what this means I don't know but could
cause weird problems in if statements using floating point results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top