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

I want to correct Round function

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)

Nov 21 '05 #1
17 5619
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

"nomenklatura" wrote:
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)


Nov 21 '05 #2
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
Nov 21 '05 #3
"nomenklatura" <il******@operamail.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
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).
Nov 21 '05 #4
"squig" <sq************@midsouth.rr.com> wrote in message
news:KN*******************@fe2.columbus.rr.com...
"nomenklatura" <il******@operamail.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
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).


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.
Nov 21 '05 #5
I am in Turkey.
We pass new Currency unit at new year.. (TL->YTL).
http://www.ytl.gen.tr/ytl/index_eng.php
The Ministry of Finance want to round this format..
That is not important odd or even..

And still i couldn't use round function..

"Cor Ligthert" <no************@planet.nl>, iletide şunu yazdı
news:uu**************@TK2MSFTNGP10.phx.gbl...
Nov 21 '05 #6
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

"nomenklatura" <il******@operamail.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
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)


Nov 21 '05 #7
i solved with FormatNumber functions
thanks all

Nov 21 '05 #8
"nomenklatura" <il******@operamail.com> schrieb:
System.Math.Round function is confused me.


In addition to the other replies:

How To Implement Custom Rounding Procedures
<URL:http://support.microsoft.com/?scid=kb;EN-US;196652>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9
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. :(
Nov 21 '05 #10
"Randy Given" <Gi********@aol.com> wrote in message
news:73swd.6024$sf5.840@lakeread05...
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.
Nov 21 '05 #11
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
Nov 21 '05 #12
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OU**************@TK2MSFTNGP14.phx.gbl...
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.
Nov 21 '05 #13
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
Nov 21 '05 #14
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
"squig" <sq************@midsouth.rr.com> wrote in message
news:X9****************@fe1.columbus.rr.com...
"Randy Given" <Gi********@aol.com> wrote in message
news:73swd.6024$sf5.840@lakeread05...
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.

Nov 21 '05 #15
"Cor Ligthert" <no************@planet.nl> wrote in message
news:#R**************@TK2MSFTNGP11.phx.gbl...
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


Wasn't picking on words -- I'm trying to understand the reasoning behind it
being more accurate.
Nov 21 '05 #16
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eG**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 21 '05 #17
> 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.
Nov 21 '05 #18

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

Similar topics

3
by: ORC | last post by:
Is there a round function that do a "Normal" rounding and not the round to nearest as the Math.Rounds is? Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); //Returns 3.4....
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
5
by: Marc | last post by:
Hi, I cannot get the round function to work on vb.net. I get the message that round is not declared? Has round function changed or something? MsgBox(round(3, 3))
2
by: shashiramu | last post by:
Hi there, Am trying to round a value 36.825 to 36.82, But if i use ROUND function it is giving me 36.83 so can anyone help me in getting the value i needed Thanks in Advance Shashi
9
by: josh logan | last post by:
Hello, I need a round function that _always_ rounds to the higher integer if the argument is equidistant between two integers. In Python 3.0, this is not the advertised behavior of the built-in...
0
by: Maric Michaud | last post by:
Le Monday 28 July 2008 02:35:08 Herman, vous avez écrit : Hmm, I don't have the same result in python2.6, I suspect it's a floating point precision problem, try just to type "0.5" in the console...
1
by: RiK ooo | last post by:
Hi, i'm currently working on C# project and I want to make use of the Math.Round() function to display only the first to digits of a float value. Unfortunately this doesn't seem to be working for me....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.