I want to correct Round function

N

nomenklatura

Hi,
System.Math.Round function is confused me.
for example i want to round 3.245 in with decimal symbol
Result should be = 3.25

When i try to this in vb:
A = 3.245
X = Round(A, 2)
then x=3.24 , result is is false

But when i try to A = 3.235
X = Round(A, 2)
then x=3.24 , result is true

So how can i trust this function.
Or are there any true round function which you know?
Not: Normally , I want to round with two decimal place (>=5 is round up, <5
is round down)
 
G

Guest

Nomenklatura,

I think you can trust the Round method to always act according to the
documentation:

"If value is halfway between two numbers, one of which is even and the other
odd, then the even number is returned."

"The behavior of this method follows IEEE Standard 754, section 4. This kind
of rounding is sometimes called rounding to nearest, or banker's rounding."

Kerry Moorman
 
C

Cor Ligthert

Kerry,
"If value is halfway between two numbers, one of which is even and the
other
odd, then the even number is returned."

"The behavior of this method follows IEEE Standard 754, section 4. This
kind
of rounding is sometimes called rounding to nearest, or banker's
rounding."
I am curious where this is used beside banking.

Is this standard used in your country and when yes, from what country are
you?

Cor
 
S

squig

nomenklatura said:
Hi,
System.Math.Round function is confused me.
for example i want to round 3.245 in with decimal symbol
Result should be = 3.25

When i try to this in vb:
A = 3.245
X = Round(A, 2)
then x=3.24 , result is is false

But when i try to A = 3.235
X = Round(A, 2)
then x=3.24 , result is true

So how can i trust this function.
Or are there any true round function which you know?
Not: Normally , I want to round with two decimal place (>=5 is round up, <5
is round down)

In this instance, add 0.005 and truncate. I would suggest writing your own
round function to handle various decimal places (e.g., MyRound, or
whatever).
 
S

squig

squig said:
In this instance, add 0.005 and truncate. I would suggest writing your own
round function to handle various decimal places (e.g., MyRound, or
whatever).

One caveat -- be careful using FormatNumber since it returns a character
string rather than numeric. If you have to do further computation with your
(rounded) number, you will need to convert it back to a numeric.
 
J

Jay B. Harlow [MVP - Outlook]

nomenklatura,
It appears that VS.NET 2005 (aka Whidbey, due out later in 2005) adds
overloads to Math.Round to support current "banker's rounding" or the more
conventional "round up".

http://msdn2.microsoft.com/library/ef48waz8.aspx

Otherwise as squig suggests you probably want to create your own rounding
routine if "banker's rounding" is not appropriate for your routine...

Hope this helps
Jay
 
R

Randy Given

That is round-toward-even and I wish everyone would use it (it is more
accurate). Unfortunately, it is even harder than converting people to metric
systems. :(
 
S

squig

Randy Given said:
That is round-toward-even and I wish everyone would use it (it is more
accurate). Unfortunately, it is even harder than converting people to metric
systems. :(

Not trying to be argumentative but please explain how it is more accurate --
maybe I'm not fully understanding your comment.
 
C

Cor Ligthert

Squig,

Why you do else think they call it "banking" system.

The way that is always been used is alway for 4 figurs down(1,2,3,4) and for
5 up(5,6,7,8,9)

That is eliminated in this system too an equal situations where it is 4,5
times down and 4,5 times up.

However as Randy said it is hardly used.

Probably because you need a computer to use it.


Cor
 
S

squig

Cor Ligthert said:
Squig,

Why you do else think they call it "banking" system.

The way that is always been used is alway for 4 figurs down(1,2,3,4) and for
5 up(5,6,7,8,9)

That is eliminated in this system too an equal situations where it is 4,5
times down and 4,5 times up.

However as Randy said it is hardly used.

Probably because you need a computer to use it.


Cor

Okay, but it still doesn't explain how it is more accurate. I understand the
concept.
 
C

Cor Ligthert

Squig,
Okay, but it still doesn't explain how it is more accurate. I understand
the
concept.
A rounding is never accurate. However, in this newsgroup with so many
persons whose native language is not English, it is used that you try to
understand, not to pick on words.

Just my thought,

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Squig,
The way I understand it is:

Rather then always rounding the mid point up, "half of the time" you are
rounding it up & "half of the time" you are rounding it down.

The two "half of the time" together will average themselves out of the
picture.

When you always rounding the mid point up, then you start accumulating all
the fractions, rather then average them out...

The following demonstrates this:

Public Shared Sub Main()
Dim value1 As Decimal = 1.235D
Dim value2 As Decimal = 1.245D

Dim value3 As Decimal = value1 + value2

Dim value4 As Decimal = Decimal.Round(value1, 2) +
Decimal.Round(value2, 2)
Dim value5 As Decimal = RoundUp(value1, 2) + RoundUp(value2, 2)

Debug.WriteLine(value1, "value1")
Debug.WriteLine(value2, "value2")
Debug.WriteLine(value3, "value3")
Debug.WriteLine(value4, "value4")
Debug.WriteLine(value5, "value5")
End Sub

' may not handle negative value correctly...
Private Shared Function RoundUp(ByVal value As Decimal, ByVal decimals
As Integer) As Decimal
decimals = CInt(10 ^ decimals)
value *= decimals
value = Decimal.Truncate(value + 0.5D)
value /= decimals
Return value
End Function

Notice how value4 & value5 are off by 1, if you are lot of rounding, this
can add up significantly. When you use banking rounding, this difference
will not add up as quickly or as much...

Hope this helps
Jay
 
S

squig

Cor Ligthert said:
Squig,
A rounding is never accurate. However, in this newsgroup with so many
persons whose native language is not English, it is used that you try to
understand, not to pick on words.

Just my thought,

Cor

Wasn't picking on words -- I'm trying to understand the reasoning behind it
being more accurate.
 
S

squig

Jay B. Harlow said:
Squig,
The way I understand it is:

Rather then always rounding the mid point up, "half of the time" you are
rounding it up & "half of the time" you are rounding it down.

The two "half of the time" together will average themselves out of the
picture.

When you always rounding the mid point up, then you start accumulating all
the fractions, rather then average them out...

The following demonstrates this:

Public Shared Sub Main()
Dim value1 As Decimal = 1.235D
Dim value2 As Decimal = 1.245D

Dim value3 As Decimal = value1 + value2

Dim value4 As Decimal = Decimal.Round(value1, 2) +
Decimal.Round(value2, 2)
Dim value5 As Decimal = RoundUp(value1, 2) + RoundUp(value2, 2)

Debug.WriteLine(value1, "value1")
Debug.WriteLine(value2, "value2")
Debug.WriteLine(value3, "value3")
Debug.WriteLine(value4, "value4")
Debug.WriteLine(value5, "value5")
End Sub

' may not handle negative value correctly...
Private Shared Function RoundUp(ByVal value As Decimal, ByVal decimals
As Integer) As Decimal
decimals = CInt(10 ^ decimals)
value *= decimals
value = Decimal.Truncate(value + 0.5D)
value /= decimals
Return value
End Function

Notice how value4 & value5 are off by 1, if you are lot of rounding, this
can add up significantly. When you use banking rounding, this difference
will not add up as quickly or as much...

Hope this helps
Jay

Thanks for the explanation. The last sentence sums up why, which is what I
was asking.
 
R

Randy Given

Not trying to be argumentative but please explain how it is more
accurate --
maybe I'm not fully understanding your comment.

If you have these numbers:

1.5
2.5
3.5
4.5

The real total is 12.

Rounding up (typical way) gives 14:

1.5 -> 2
2.5 -> 3
3.5 -> 4
4.5 -> 5

Rounding toward even gives 12:

1.5 -> 2
2.5 -> 2
3.5 -> 4
4.5 -> 5

See? More accurate on average.
 

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

Similar Threads


Top