Finds the numerical value that equals or is the next lowest value - CSharp System.Collections

CSharp examples for System.Collections:IEnumerable

Description

Finds the numerical value that equals or is the next lowest value

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from ww  w.  j a  va  2 s  .  co m*/

public class Main{
        /// <summary>
        /// Finds the numerical value that equals or is the next lowest value
        /// </summary>
        public static long Floor(this IEnumerable<long> array, long find)
        {
            var values = array.ToList();

            values.Sort();

            if (values.Contains(find))
                return find;

            if (values.Max() <= find)
                return values.Max();


            var ceiling = values.FirstOrDefault(value => value > find);

            var floor = values[values.IndexOf(ceiling) - 1];

            return floor;
        }
}

Related Tutorials