473,887 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Roun d(ldecWater, 0), String)

--
Chris Davoli

Apr 4 '06 #1
4 3458
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********@hotm ail.com
http://www.nathansokalski.com/
"Chris Davoli" <Ch*********@di scussions.micro soft.com> wrote in message
news:8D******** *************** ***********@mic rosoft.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.Roun d(ldecWater, 0), String)

--
Chris Davoli

Apr 4 '06 #2

"Chris Davoli" <Ch*********@di scussions.micro soft.com> wrote in message
news:8D******** *************** ***********@mic rosoft.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.Roun d(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********@hotm ail.com
http://www.nathansokalski.com/

"David Browne" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in
message news:OO******** ******@TK2MSFTN GP05.phx.gbl...

"Chris Davoli" <Ch*********@di scussions.micro soft.com> wrote in message
news:8D******** *************** ***********@mic rosoft.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.Roun d(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
5180
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 0.90000000000000013
1
4144
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 totally bullshit! i expect: Round(1.344, 2) == 123.34 Round(1.345, 2) == 123.35 and nothing else! for a workaround i had to write my own rounding method (not tested):
6
7104
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 = Math.Round( original, 2 ); But I'm not happy with the results. E.g. if original is
6
9562
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 to shine when writing accounting software :-) How are others dealing with this? -- - Mitchell Vincent
7
2456
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
16050
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) 0.74
3
5358
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 problem but it didn't. I have VS 2005 and I am using vb.net try rounding the number 4.935 to 2 decimals: What I am seeing is the following: Math.Round(4.935, 2, MidpointRounding.AwayFromZero) 'Result is 4.93 Math.Round(4.935, 2,...
4
10914
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
5626
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 to complete this part to move onto the next. <HTML> <HEAD> <TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9799
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,...
0
10771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7988
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
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4633
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
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.