Math.Round not working for a certain number...

G

Guest

The folllowing will round to 526, but it should round to 527. It works
correctly for all other numbers, except for this one. Does anybody know of a
bug in Math.Round?
Dim ldecWater As Decimal = 526.5
CType(Math.Round(ldecWater, 0), String)
 
N

Nathan Sokalski

I looked at the Documentation for Math.Round Method (Decimal, Int32) and
found the following:

Return Value
The number nearest d with precision equal to decimals. If d is halfway
between two numbers, one of which is even and the other odd, then the even
number is returned. If the precision of d is less than decimals, then d is
returned unchanged.


Because your value is halfway between, it is returning the even value, which
is 526. The documentation says that it was designed this way for the sake of
a certain IEEE standard, why that standard was made that way, I don't know.
My suggestion as a workaround would be to write a very small function of
your own that uses an if statement along with the Math.Round method, or
maybe someone else has a better idea, but this is why it gives you the
answer it does.
 
D

David Browne

Chris Davoli said:
The folllowing will round to 526, but it should round to 527. It works
correctly for all other numbers, except for this one. Does anybody know of
a
bug in Math.Round?
Dim ldecWater As Decimal = 526.5
CType(Math.Round(ldecWater, 0), String)

Per the docs, this is how Round works.

There is an overload to choose a different style of midpoint rounding.
http://msdn2.microsoft.com/en-us/library/system.math.round.aspx

Public Shared Function Round ( _ d As Decimal, _ decimals As Integer _ ) As
Decimal
.. . .
Parameters
d
A decimal number to be rounded.
decimals
The number of significant decimal places (precision) in the return value.
Return Value
The number nearest d with a precision equal to decimals. If d is halfway
between two numbers, one of which is even and the other odd, then the even
number is returned. If the precision of d is less than decimals, then d is
returned unchanged.

David
 
N

Nathan Sokalski

That sounds wonderful, but I could not find those overloads in the
documentation on my computer. I use Visual Studio .NET 2003 with .NET
Framework 1.1.4322. What versions are those overloads available in? Do they
require 2.0, are they simply not shown in the documentation of 1.1, or am I
missing something else? Thanks.
 
H

Hans Kesting

I looked at the Documentation for Math.Round Method (Decimal, Int32) and
found the following:

Return Value
The number nearest d with precision equal to decimals. If d is halfway
between two numbers, one of which is even and the other odd, then the even
number is returned. If the precision of d is less than decimals, then d is
returned unchanged.


Because your value is halfway between, it is returning the even value, which
is 526. The documentation says that it was designed this way for the sake of
a certain IEEE standard, why that standard was made that way, I don't know.

I think the reason is that IF you work a lot with "halves" (values
ending in .5) AND you add up the rounded values, THEN you will get a
value that is reasonably accurate (with this rounding method). If all
those values where rounded up, then the sum would end up way too large.

If you have all sorts of fractionals, it will average out.

Hans Kesting
 

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

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top