Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round to nearest even integer?

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 1123 Math.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?

like image 504
Álvaro García Avatar asked Sep 04 '18 11:09

Álvaro García


People also ask

How do you round to the nearest integer?

Rounding to the Nearest Integer If the digit in the tenths place is less than 5, then round down, which means the units digit remains the same; if the digit in the tenths place is 5 or greater, then round up, which means you should increase the unit digit by one.

How do you round to the nearest even number in Excel?

In order to get Excel to round toward the even integer, you would have to use =IF(MOD(A2,1)=0.5,EVEN(A2),ROUND.

Do you round 5 to the nearest even number?

1. Some statisticians prefer to round 5 to the nearest even number. As a result, about half of the time 5 will be rounded up, and about half of the time it will be rounded down.

What is the even odd rule for rounding?

The intention of the odd-even rule is to average the effects of rounding off. EVEN: If the last retained digit is even, its value is not changed, the 5 and any zeros that follow are dropped. ODD: if the last digit is odd, its value is increased by one.


2 Answers

Try this (let's use Math.Round with MidpointRounding.AwayFromZero in order to obtain "next even value" but scaled - 2 factor):

double source = 1123.0;  // 1124.0 double result = Math.Round(source / 2, MidpointRounding.AwayFromZero) * 2; 

Demo:

double[] tests = new double[] {      1.0,   1123.1,   1123.0,   1122.9,   1122.1,   1122.0,   1121.5,   1121.0, };  string report = string.Join(Environment.NewLine, tests   .Select(item => $"{item,6:F1} -> {Math.Round(item / 2, MidpointRounding.AwayFromZero) * 2}"));  Console.Write(report); 

Outcome:

   1.0 -> 2     // In case of tie, next even value 1123.1 -> 1124 1123.0 -> 1124  // In case of tie, next even value 1122.9 -> 1122 1122.1 -> 1122 1122.0 -> 1122 1121.5 -> 1122 1121.0 -> 1122  // In case of tie, next even value 
like image 90
Dmitry Bychenko Avatar answered Oct 04 '22 23:10

Dmitry Bychenko


One liner:

double RoundToNearestEven(double value) =>     Math.Truncate(value) + Math.Truncate(value) % 2; 

Fiddle

Explanation: if we have an even number with some digits after floating point, we need to just get rid of those digits. If we have an odd number, we need to do the same and then move to the next integer that is guaranteed to be even.

P.S. Thanks to @DmitryBychenko for pointing out that casting double to long is not the brightest idea.

like image 32
Dmitry Korolev Avatar answered Oct 05 '22 00:10

Dmitry Korolev