Round(0.5) returns 0.

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

Armin Zingler

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)
 
M

Michel Posseth [MCP]

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
 
C

Cor Ligthert[MVP]

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
 
T

Tom Shelton

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

Cor Ligthert[MVP]

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
 
G

Göran Andersson

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

jdrott1

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

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

needs double value in string format 6
issue of assigning a double variable in .net. 5
Private and Confidential 1
Italc 1
Barcode 7
2003 to 97 2
2007 to 2003 3
Size limit for email signature 1

Top