Math.Round returns a rounded value for odd values, but rounded for even

I'm trying to find a float using math round I found the following

0.5 --> 0
1.5 --> 2
2.5 --> 2
3.5 --> 4

etc. I believe this is due to a floating point error, but not quite sure how to do this. How can I get around this so that even the numbers are rounded correctly?

+4
source share
4 answers

From the documentation ;

, a. a , , - , . , Double .

Math.Round , MidpointRounding , , .

AwayFromZero 

, , .

ToEven

, .

+7

, , :

Math.Round(value, MidpointRounding.AwayFromZero);

:

, , .

MidpointRounding, , .

+4

 Math.Round(value, MidpointRounding.AwayFromZero);

MSDN

a , , - , .

, , , , , Microsoft IEEE 754. MSDN Math.Round , :

, - ; , () , 50% ; .

, - ; , ( ) ( ); .

+1

This is called banker rounding (rounded to even). You can read about it here . This is a feature of the .NET Framework and works as designed.

0
source

All Articles