Round(0.5) returns 0.

  • Thread starter Thread starter Valli
  • Start date Start date
V

Valli

Hi,

Dim d as double
d= 0.5
Round(d) returns 0

d= 1.5
Round(d) returns 2

I need Round(0.5) = 1. Is it possible in .net?


--
Thanks & regards,

V.Vallikkannu



This e-mail and any files transmitted with it are for the sole use of the
intended recipient(s) and may contain confidential and privileged
information. If you are not the intended recipient, please contact the
sender by reply e-mail and destroy all copies of the original message.

Any unauthorized review, use, disclosure, dissemination, forwarding,
printing or copying of this email is strictly prohibited and may be
unlawful.
 
Am 27.03.2010 10:57, schrieb Valli:
Hi,

Dim d as double
d= 0.5
Round(d) returns 0

d= 1.5
Round(d) returns 2

I need Round(0.5) = 1. Is it possible in .net?

I don't know which Round function you are using here, without a
qualifier. Try Math.Round:

d = Math.Round(d, MidpointRounding.AwayFromZero)
 
For what it`s worth ,,

The Round function goes to the nearest integer, and if there are two nearest
integers then it goes to the even one. 1.5 rounds to 2, 0.5 rounds to 0.

The behavior you noticed is not specific to .Net , it is called "Bankers
rounding" and it is the standard rounding method used in every programming
language
dating back to COBOL and maybe even further ( maybe Cor can tell us that
-) )

The rounding that you want is mathmetical rounding as how you have learned
to round in basic scholar math courses
this rounding method is midpointrounding away from zero

http://blogs.msdn.com/ericlippert/archive/2003/09/26/bankers-rounding.aspx


HTH

Michel
 
Michel,

Rounding was in the beginning not build in, you had to create your own
mathematical functions for that.

I was not aware before Net about Bankers rounding, can be lack of my
knowledge but that is simply the truth

Cor
 
Michel,

Rounding was in the beginning not build in, you had to create your own
mathematical functions for that.

I was not aware before Net about Bankers rounding, can be lack of my
knowledge but that is simply the truth

VB has used bankers rounding by default for as long as I can remember.
 
Tom,

Explicitly you write in my idea that the Net System values and methods are
build around Visual Basic (in what I not doubt) and not around C#.

And that from your pen

:-)

Cor
 
Cor said:
Tom,

Explicitly you write in my idea that the Net System values and methods
are build around Visual Basic (in what I not doubt) and not around C#.

And that from your pen

:-)

Cor

....and as usual you have a strange idea about what people write.

Just because one single concept that exists in the .NET framework also
exists in VB, doesn't mean that everything in the framework comes from VB.
 
...and as usual you have a strange idea about what people write.

Just because one single concept that exists in the .NET framework also
exists in VB, doesn't mean that everything in the framework comes from VB..








--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

if you are taking about a label or textbox for output it would be:

lbl.text = format (your calc or variable, "0.00")

if you want to round for calculation, who cares... fix it on output!
 
Back
Top