c# 多數值區間判斷是否有重疊

來源:互聯網
上載者:User

標籤:func   private   with   交集   sum   表示   amp   special   only   

/// <summary>    /// 金額區間判斷協助類     /// </summary>    public static class DecimalRangeHelper    {        /// <summary>        /// 是否有交集        /// </summary>        /// <param name="currentRange"></param>        /// <param name="otherRange"></param>        /// <returns></returns>        public static bool IsIntersectionWith(this DecimalRange currentRange, DecimalRange otherRange)        {            return currentRange.Min.In(otherRange.Min, otherRange.Max) || currentRange.Max.In(otherRange.Min, otherRange.Max) && otherRange.Min != currentRange.Max;        }        /// <summary>        /// 判斷金額區間存在交集        /// </summary>        /// <param name="currentRanges"></param>        /// <returns></returns>        public static bool ExistsIntersectionRange(this List<DecimalRange> currentRanges)        {            return currentRanges.Any(p => currentRanges.Where(q => !object.ReferenceEqual(p,q)).Any(z => p.IsIntersectionWith(z)));        }    }

 

/// <summary>    /// 金額區間對應類     /// </summary>    public class DecimalRange    {        /// <summary>        /// 最大        /// </summary>        private decimal max;        /// <summary>        /// 最小值        /// </summary>        public decimal Min { get; set; }        /// <summary>        /// 最大值        /// </summary>        public decimal Max        {            get            {                return (max == 0) ? Decimal.MaxValue : max;            }            set            {                max = value;            }        }        /// <summary>        /// ToString        /// </summary>        /// <returns></returns>        public override string ToString()        {            return Min.ToString()+"-"+Max.ToString();        }    }

 

 /// <summary>    /// 金額協助類    /// </summary>    public static class DecimalHelper    {        /// <summary>        /// 判斷指定金額是否在指定金額範圍內        /// </summary>        public static readonly Func<decimal, decimal, decimal, bool> IsInDecimalPeriodByMomney = (current, min, max) => min <= current && max > current;        /// <summary>        /// 判斷指定金額是否在指定金額範圍內        /// </summary>        public static bool In(this decimal current, decimal min, decimal max)        {            return IsInDecimalPeriodByMomney(current, min, max);        }        /// <summary>        /// 判斷指定金額範圍是否包含指定金額範圍內(max=0時表示不限制)        /// </summary>        public static bool InSpecial(this decimal currentMin, decimal currentMax, decimal min, decimal max)        {            if (max == 0)            {                max = Decimal.MaxValue;            }            if (currentMax == 0)            {                currentMax = Decimal.MaxValue;            }            return currentMin.In(min, max);        }    }

 

c# 多數值區間判斷是否有重疊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.