Quantcast
Channel: How to round to nearest even integer? - Stack Overflow
Viewing all articles
Browse latest Browse all 6

How to round to nearest even integer?

$
0
0

My last goal is always to round to the nearest even integer.

For example, the number 1122.5196 I want as result 1122. I have tried this options:

Math.Round(1122.5196d, 0, MidpointRounding.ToEven);       // result 1123Math.Round(1122.5196d, 0, MidpointRounding.AwayFromZero); // result 1123

At the end, what I would like to get it is always the nearest even integer. For example:

  • 1122.51 --> 1122
  • 1122.9 --> 1122 (because the nearest int is 1123 but it is odd, and 1122 is nearer than 1124)
  • 1123.0 --> 1124 (the next even value, the next higher even value)

I only work with positive numbers.

And so on.

There are some method that do that or I should to implement my own method?


Viewing all articles
Browse latest Browse all 6

Trending Articles