Rounding Issue

A

AMDRIT

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
 
L

Larry Lard

AMDRIT said:
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.
 
A

Anthony Jones

AMDRIT said:
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.
 
A

AMDRIT

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.AwayFromZero)=163
 
R

RobinS

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.
--------------------------
 

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

Top