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

Rounding Issue

Given:

?System.Math.Round(161.5D,0)
162.0
?System.Math.Round(162.5D,0)
162.0
?System.Math.Round(163.5D,0)
164.0

In either C# '05 or VB.Net '05, can anyone explain the inconsistancy? Is
there a better way to round a value in a consistant manner?

I have created a function for it until I have a better way.

''' <summary>
''' Rounds a decimal value to the nearest whole number integer.
''' </summary>
''' <param name="numberToRound"></param>
''' <returns></returns>
Public Shared Function Round(ByVal numberToRound As Decimal) As Integer

' Rounds a value to the nearest whole number integer
' inputs:
' 1. ByVal Decimal
' return: Long

Dim bNegative As Boolean
Dim iResult As Decimal

'Flag to let us know we were working with a negative number
bNegative = numberToRound < 0

'Check to see if the decimal portion is greater than or equal to .5
If Abs(Abs(numberToRound) - Int(Abs(numberToRound))) >= 0.5D Then
'Round up
iResult = -Int(-(Abs(numberToRound)))
Else
'Round down
iResult = Abs(numberToRound) - (Abs(numberToRound) -
Int(Abs(numberToRound)))
End If

'Check our flag and convert to negative accordingly
If bNegative Then iResult = -iResult

Return iResult

End Function
Jan 19 '07 #1
4 3379
AMDRIT wrote:
Given:

?System.Math.Round(161.5D,0)
162.0
?System.Math.Round(162.5D,0)
162.0
?System.Math.Round(163.5D,0)
164.0

In either C# '05 or VB.Net '05, can anyone explain the inconsistancy?
What inconsistency? The method is behaving exactly as specified in its
documentation:

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

In 2.0, Math.Round has another overload which allows you to specify an
alternate Midpointrounding behavior, if you want. It's in the docs.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jan 19 '07 #2

"AMDRIT" <am****@hotmail.comwrote in message
news:O4**************@TK2MSFTNGP06.phx.gbl...
Given:

?System.Math.Round(161.5D,0)
162.0
?System.Math.Round(162.5D,0)
162.0
?System.Math.Round(163.5D,0)
164.0

In either C# '05 or VB.Net '05, can anyone explain the inconsistancy? Is
there a better way to round a value in a consistant manner?
It is consistent exact .5 fractions always round toward the even number.

The reasoning is rounding a figure that has an exact .5 fraction in a
specific direction either always down or always up would introduce a
statistical skew over a large sample.



Jan 19 '07 #3
I didn't realize that rounding to even was the default. IEEE Standard 754,
section 4 is very daunting, even on Wikipedia. Is there anyway to default
such a setting at the assembly level? Is there anyway to use CInt and CLng
and specify that criteria as well?

system.Math.Round(162.5D,0,MidpointRounding.AwayFr omZero)=163
"Larry Lard" <la*******@googlemail.comwrote in message
news:51*************@mid.individual.net...
AMDRIT wrote:
>Given:

?System.Math.Round(161.5D,0)
162.0
?System.Math.Round(162.5D,0)
162.0
?System.Math.Round(163.5D,0)
164.0

In either C# '05 or VB.Net '05, can anyone explain the inconsistancy?

What inconsistency? The method is behaving exactly as specified in its
documentation:

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

In 2.0, Math.Round has another overload which allows you to specify an
alternate Midpointrounding behavior, if you want. It's in the docs.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version

Jan 19 '07 #4
Just FYI, it's called "banker's rounding".

It was a real pain in the a** (to me) in VB6. I ended up
using a different rounding routine written by Getz or Litwin
or one of those guys.

Robin S.
--------------------------
"AMDRIT" <am****@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I didn't realize that rounding to even was the default. IEEE Standard
754, section 4 is very daunting, even on Wikipedia. Is there anyway to
default such a setting at the assembly level? Is there anyway to use
CInt and CLng and specify that criteria as well?

system.Math.Round(162.5D,0,MidpointRounding.AwayFr omZero)=163
"Larry Lard" <la*******@googlemail.comwrote in message
news:51*************@mid.individual.net...
>AMDRIT wrote:
>>Given:

?System.Math.Round(161.5D,0)
162.0
?System.Math.Round(162.5D,0)
162.0
?System.Math.Round(163.5D,0)
164.0

In either C# '05 or VB.Net '05, can anyone explain the
inconsistancy?

What inconsistency? The method is behaving exactly as specified in
its documentation:

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

In 2.0, Math.Round has another overload which allows you to specify
an alternate Midpointrounding behavior, if you want. It's in the
docs.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version


Jan 19 '07 #5

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

Similar topics

7
by: Steven T. Hatton | last post by:
I'm surprised I haven't hit this situation till now, but I don't believe I've had to deal with it before. I have a function that sets the components of a point class (QPoint from Qt). It takes...
6
by: djc | last post by:
I have a sql field (MSDE 2000) of type Decimal. When inserting a value into the field from an asp.net web form I am using the CDec() function. If I enter a 2.1 through a 2.4 a 2.00 is entered into...
6
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp ---------------------...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
5
by: Cygnus | last post by:
Sorry in advance for the lack of formatting in this posting. Data: (column headers) Net Sales | Royalty Rate | Total Royalty (data) 4.31 | 50.00% | 2.15 19.35 | 50.00% | 9.68
4
by: Altman | last post by:
I am having a rounding problem all over the place and am struggling to fix it. My problem is that I have an unit price and a quantity. When I multiply them together they come out to 3.705. I...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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,...
0
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...

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.