473,836 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem With Round Function

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(dblN um)
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(dblN um, 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Tex t
x = strString.Index Of(",")
If x = -1 Then
dblNum = Val(strString)
dblAnswer = Math.Round(dblN um)
Else
dblNum = Val(strString.S ubstring(0, x))
IntDecPt = Val(strString.S ubstring(x + 1, 1))
dblAnswer = Math.Round(dblN um, IntDecPt)
End If
Me.Label1.Text = dblAnswer
End Sub
Ron

--
Ronald W. Roberts
Roberts Communication
rw*@robcom.com

Nov 21 '05 #1
9 7389

Ronald W. Roberts wrote:
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(dblN um, 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(dblN um, 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.5 5, 1)
'this gives 2.6, following the round-to-even rule
?Math.Round(2.5 5!, 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(Sing le), 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.5500001907348 633, for example).

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

--
Larry Lard
Replies to group please

Nov 21 '05 #2
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
Nov 21 '05 #3
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
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:OE******** ******@TK2MSFTN GP10.phx.gbl...
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

Nov 21 '05 #4
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
Nov 21 '05 #5
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
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:Ow******** ******@TK2MSFTN GP12.phx.gbl...
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

Nov 21 '05 #6
In message <er************ *@TK2MSFTNGP15. phx.gbl>, Charles Law
<bl***@nowhere. com> writes
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

Charles
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:Ow******* *******@TK2MSFT NGP12.phx.gbl.. .
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



--
Kevin
Nov 21 '05 #7
Hi Kevin

One to add to my helper functions :-)

Charles
"kevin" <ke***@NOsoltec SpiceD.HaMdemon .co.uk> wrote in message
news:Sp******** ******@soltec.d emon.co.uk...
In message <er************ *@TK2MSFTNGP15. phx.gbl>, Charles Law
<bl***@nowhere. com> writes
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

Charles
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:Ow****** ********@TK2MSF TNGP12.phx.gbl. ..
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



--
Kevin

Nov 21 '05 #8
Ronald W. Roberts wrote:
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(dblN um)
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(dblN um, 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Tex t
x = strString.Index Of(",")
If x = -1 Then
dblNum = Val(strString)
dblAnswer = Math.Round(dblN um)
Else
dblNum = Val(strString.S ubstring(0, x))
IntDecPt = Val(strString.S ubstring(x + 1, 1))
dblAnswer = Math.Round(dblN um, 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

--
Ronald W. Roberts
Roberts Communication
rw*@robcom.com

Nov 21 '05 #9
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.

"Ronald W. Roberts" wrote:
Ronald W. Roberts wrote:
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(dblN um)
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(dblN um, 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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.Tex t
x = strString.Index Of(",")
If x = -1 Then
dblNum = Val(strString)
dblAnswer = Math.Round(dblN um)
Else
dblNum = Val(strString.S ubstring(0, x))
IntDecPt = Val(strString.S ubstring(x + 1, 1))
dblAnswer = Math.Round(dblN um, 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

--
Ronald W. Roberts
Roberts Communication
rw*@robcom.com

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1590
by: erica | last post by:
Hi, I am having a very strange problem. I wrote a program in php 4.3.10 that uses objects. My classes.inc file looks like this: <?php // Coordinate class class Coord {
3
2048
by: Robert Dell | last post by:
I have a problem comparing strings in an order form i'm writing. I want to give a running total at the bottom of the page and it appears to be working except it doesn't compare correctly (it always adds things up when you didn't select them) so i only added 2 of the items until I got it worked out. here's the page source (you can look at it yourself at http://robertdell.dyndns.org/shifts/ <!DOCTYPE HTML PUBLIC> <html> <head>
3
2559
by: mg | last post by:
Hi everybody... We try to white scripts with Pyrhon 2.4 for an acoustic simulation and we wrote these follow lines : <begin script> c = 340 i =j=k= 1 sum_ = 23 table =
14
4635
by: m | last post by:
all, i am trying to use the function round() which I found through google to be declared in math.h ( http://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html). this function does not work and i get an unreferenced symbol error. the function rint() works when math.h is included. is round() not found in all math.h files? if so what might be an alternative. i work on a machine that runs a sparc-sun-solaris operating system...
8
1645
by: Chris Prior | last post by:
Since the last update for office 2003, i've been having issues with the int() function in vb (version 9972 vba retail 6.4.992) The accounting software that the business i work for writes for dozens of clients regularly uses x = (int(x*100))/100 to ensure that there are no fractional cents on the end of prices. It works wonderfully, until one of a number of values are used. 8.95, 9.95, 2094.95, and a host of other seemingly random...
3
2308
by: Mark Szlazak | last post by:
The following page simulates a pool cue and cue ball: http://members.aol.com/myscript/cue.html Mouse cursor position around the cue ball determines where a roll-over of 179 pool cue images is placed around the ball and which one of those images is displayed. Each pool cue image is in a slightly different orientation and the correct one is chosen to match the orientation of the cursor around the ball. Holding down the left mouse button...
1
5385
by: lindabaldwin | last post by:
Hi everyone, I have a code that I can use in Excel to round to the nearest 0.5, but I cannot get it to work in Access. When I try to execute it I get the following error message: "Compile error: user defined type not defined." Any idea what I need to do to change my code to work in Access? Here's the code: 'Round to nearest 0.5 Public Function vbaRoundTO(dblValue As Double, dblRoundTo As Double, _ Optional RoundingOption As rOpt...
16
1949
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi All, I have a neat little script that calculates price based on quantity without refreshing the page.. the script is - <script type="text/javascript"> function OpenWin(url) { window.open(url,'win','scrollbars=1,status=0,resizable=0,width=200,height=265'); }
1
1352
by: Adrienne Boswell | last post by:
Gazing into my crystal ball I observed =?Utf-8?B?R1ROMTcwNzc3?= <GTN170777@discussions.microsoft.comwriting in news:443E2509-7F0E-4CEC-B243-D6EDC931DB7F@microsoft.com: ASP has no knowledge of the client, this is a client side issue. Followups set to comp.lang.javascript. 0,widt
0
9673
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10260
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9388
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4463
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.