473,404 Members | 2,137 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,404 software developers and data experts.

Math.Round broken?

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):

public static decimal Round(decimal val, int decimals)
{
decimal factor = (val * (decimal)Math.Pow(10, 2));
decimal tmp = Math.Floor(factor);
if (val - tmp >=0.5m)
tmp++;
return tmp/factor;
}

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]
Jul 21 '05 #1
1 4100
Bankers rounding is also known as IEEE Standard 754, section 4.

Because "5" is a special case when rounding (we don't know whether it should
be rounded up or down), bankers rounding gives a better distribution of
rounding for a larger set of values.

"5" is a special case because the digits 1,2,3,4 should be rounded down and
the digits 6,7,8,9 should be rounded up. If we always rounded 5 up, this
would mean that on average, more numbers would be rounded up. Bankers
rounding gets around this by basing the rounding of "5" on whether the
number is odd or even. Thus on average 5 will be rounded up half the time
and rounded down the other half.

Hope my explanation has clarified it enough so it won't be called total
bullshit. I'd hate it if most software (and CPU's!) were bullshit ;-)
Trev.

"cody" <de********@gmx.net> wrote in message
news:#e**************@TK2MSFTNGP10.phx.gbl...
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):

public static decimal Round(decimal val, int decimals)
{
decimal factor = (val * (decimal)Math.Pow(10, 2));
decimal tmp = Math.Floor(factor);
if (val - tmp >=0.5m)
tmp++;
return tmp/factor;
}

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]

Jul 21 '05 #2

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

Similar topics

2
by: Nicolas | last post by:
Why does for the same code I got different result the VB code gave me what I want why not the csharp? Thank you for your help... CSHARP CODE int sx, sy; double sdegrees; sdegrees = (90 -...
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...
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...
4
by: Chris Davoli | last post by:
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 =...
8
by: David Wade | last post by:
Folks, Well I am still dabling in the mire of math.h and getting on reasonably well. A couple of questions. Firstly when defining some of the extreme values in the many bits of code seem to use...
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)...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
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 !!!!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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...
0
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...
0
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...
0
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,...

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.