Rounding a value only by a list of defined values โ€‹โ€‹in C #

I have a list of double values, I want to round the value of the variable only to that list of numbers

Example:

List Content: {12,15,23,94,35,48}

The value of the variable is 17, so it will be rounded to 15

If the value of the variable is less than the smallest number, it will be rounded to it; if it is greater than the largest number, it will be rounded to it.

The contents of the list always change depending on an external factor. Therefore, I cannot limit the values โ€‹โ€‹that I want to round up or down.

How to do it in C #?

+7
source share
6 answers

The LINQ method is used here:

var list = new[] { 12, 15, 23, 94, 35, 48 }; var input = 17; var diffList = from number in list select new { number, difference = Math.Abs(number - input) }; var result = (from diffItem in diffList orderby diffItem.difference select diffItem).First().number; 

EDIT : renamed some of the variables so that the code is less confusing ...

EDIT

The list variable is an implicit declaration of an int array. The first LINQ diffList defines an anonymous type with the source number from the list ( number ), as well as the difference between it and your current value ( input ).

The second LINQ result statement orders the collection of the anonymous type by difference, which is your rounding requirement. It takes the first item in this list, as it will have the smallest difference, and then selects only the original .number from the anonymous type.

+8
source

Assuming the array is sorted, you can perform a binary search in the array by narrowing it down to which two numbers of a given number are between.

Then, when you have these two numbers, you simply round to the nearest one.

 static int RoundToArray(int value, int[] array) { int min = 0; if (array[min] >= value) return array[min]; int max = array.Length - 1; if (array[max] <= value) return array[max]; while (max - min > 1) { int mid = (max + min) / 2; if (array[mid] == value) { return array[mid]; } else if (array[mid] < value) { min = mid; } else { max = mid; } } if (array[max] - value <= value - array[min]) { return array[max]; } else { return array[min]; } } 
+7
source

Using linq:

 int value = 17; var values = new float[] { 12, 15, 23, 94, 35, 48 }; if(value < values.First()) return value.First(); if(value > values.Last()) return value.Last(); float below = values.Where(v => v <= value).Max(); float above = values.Where(v => v >= value).Min(); if(value - below < above - value) return below; else return above; 

As long as the number of possible values โ€‹โ€‹is pretty small, this should work. If you have thousands of possible values, you should use another solution that uses values sorting (if it really is sorted).

+2
source

Do something like this:

 double distance = double.PositiveInfinity; float roundedValue = float.NaN; foreach (float f in list) { double d = Math.Abs(d - f); if (d < distance) { distance = d; roundedValue = f; } } 
+2
source

You can scroll through an array of numbers and set the roundedNum variable equal to each if the delta variable is less than the current lower delta. Some things are best described in code.

 int roundedNum = myNum; int delta = myArray[myArray.Length-1] + 1; for(int i=0; i<myArray.Length; ++i) { if(Math.Abs(myNum - myArray[i]) < delta) { delta = Math.Abs(myNum - myArray[i]); roundedNum = myArray[i]; } } 

That should do the trick well.

+1
source

Simple rounding in linq

  public decimal? RoundDownToList(decimal input, decimal[] list) { var result = (from number in list where number <= input orderby number descending select number).FirstOrDefault(); return result; } 
0
source

Source: https://habr.com/ru/post/899903/


All Articles