473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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)

--
Chris Davoli

Apr 4 '06 #1
4 3414
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
"Chris Davoli" <Ch*********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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)

--
Chris Davoli

Apr 4 '06 #2

"Chris Davoli" <Ch*********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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/lib...ath.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
Apr 4 '06 #3
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:OO**************@TK2MSFTNGP05.phx.gbl...

"Chris Davoli" <Ch*********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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/lib...ath.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

Apr 5 '06 #4
> 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
Apr 5 '06 #5

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

Similar topics

89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
1
by: cody | last post by:
the documentation states that Math.Round supports banker's rounding but it also states that if the last number is 5 it will rounded up if the whole number is even, otherwise rounded down which is...
6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
6
by: Mitchell Vincent | last post by:
Just making sure I'm not missing the boat here, but are there any special routines for doing currency math (fixed precision stuff) in .NET? The wonderful problems of doing math on decimals tend...
7
by: bravesplace | last post by:
Hello, I am using the folling funtion to round a number to a single digit on my form: function round1(num) { return Math.round(num*1)/1 }
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
3
by: Altman | last post by:
OK I was having rounding problems before and I didn't realize that their was a third parameter in the round function that would tell it if it a 5 to round up. I thought adding this would fix the...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
by: DaiOz | last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please, The code I have so far is below but I don't think its right please help as I need...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.