I am trying to found a float using math round I found the following
0.5 --> 0
1.5 --> 2
2.5 --> 2
3.5 --> 4
and so on. I believe this is due to floating point error, but not quite sure how. How can I get around this so even numbers round properly?
From documentation;
The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that this method returns a Double instead of an integral type.
Math.Round
method has some overloads that takes MidpointRounding
as a parameter which you can specify the rounding value if it is midway between two numbers.
AwayFromZero
When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.
ToEven
When a number is halfway between two others, it is rounded toward the nearest even number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With