/// <summary> /// Compares two doubles and determines which double is bigger. /// a < b -> -1; a ~= b (almost equal according to parameter) -> 0; a > b -> +1. /// </summary> /// <param name="a">The first value.</param> /// <param name="b">The second value.</param> /// <param name="maximumAbsoluteError">The absolute accuracy required for being almost equal.</param> public static int CompareTo(this double a, double b, double maximumAbsoluteError) { // NANs are equal to nothing, // not even themselves, and thus they're not bigger or // smaller than anything either if (double.IsNaN(a) || double.IsNaN(b)) { return a.CompareTo(b); } // If A or B are infinity (positive or negative) then // only return true if first is smaller if (double.IsInfinity(a) || double.IsInfinity(b)) { return a.CompareTo(b); } // If the numbers are equal to within the number of decimal places // then there's technically no difference if (AlmostEqual(a, b, maximumAbsoluteError)) { return 0; } // The numbers differ by more than the decimal places, so // we can check the normal way to see if the first is // larger than the second. return a.CompareTo(b); }
/// <summary> /// 如果 值 小于 min 或 大于 max 则引发 ArgumentOutOfRangeException 异常 /// </summary> /// <param name="value">参数值</param> /// <param name="argumentName">参数名称</param> /// <param name="min">最小值</param> /// <param name="max">最大值</param> public static void ThrowIfOutOfRange(this IComparable value, string argumentName, object min, object max) { if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0) { throw new ArgumentOutOfRangeException(argumentName); } }
/// <summary> /// 校验double值的有效性,如果返回false,这个值不可用,不能参与计算。 /// </summary> /// <param name="value"></param> /// <returns></returns> public static bool ValueCheck(this double value) { if (double.IsNaN(value) || double.IsInfinity(value) || value.CompareTo(double.Epsilon) == 0 || value.CompareTo(double.MaxValue) == 0 || value.CompareTo(double.MinValue) == 0) { return false; } else { return true; } }
public static bool EnhancedCompareTo(this string itemValue, Criteria criteria) { switch (criteria.Operator) { case Operators.Equal: return itemValue.CompareTo(criteria.Value) == 0; case Operators.NotEqual: return itemValue.CompareTo(criteria.Value) != 0; case Operators.StartsWith: return itemValue.StartsWith(criteria.Value); case Operators.Contains: return itemValue.Contains(criteria.Value); } return false; }
public static bool IsAssignableFrom(this TypeReference baseType, TypeReference type, Action<string> logger = null) { if (type.IsGenericParameter) return baseType.CompareTo(type); return baseType.Resolve().IsAssignableFrom(type.Resolve(), logger); }
/// <summary> /// Returns true if the DateTime is within X number of days, false otherwise. /// </summary> /// <param name="dt"></param> /// <param name="days"></param> /// <param name="useUtc"></param> /// <returns></returns> public static bool IsWithinXDays(this DateTime dt, int days, bool useUtc = true) { DateTime past = DateTime.Today.AddDays(-days).ToUniversalTime(); if (!useUtc) past = DateTime.Today.AddDays(-days); return dt.CompareTo(past) >= 0; }
/// <summary> /// Indicates whether the instance is greater as the reference value. /// </summary> /// <param name="value">The instance to test.</param> /// <param name="referenceValue">The reference value to test.</param> /// <returns><strong>true</strong> if the instance is greater as the reference value; /// otherwise, <strong>false</strong>.</returns> /// <exception cref="ArgumentNullException"><em>value</em> or <em>referenceValue</em> is /// <strong>null</strong>.</exception> public static bool IsGreater(this IComparable value, IComparable referenceValue) { Precondition.IsNotNull(value, nameof(value)); Precondition.IsNotNull(referenceValue, nameof(referenceValue)); return value.CompareTo(referenceValue) == 1; }
internal static int SafeCompareTo(this decimal val, decimal? other) { if (other == null) { return -1;//treat null as less } int compare = val.CompareTo(other.Value); return compare; }
public static bool IsBigger(this IComparable object1, IComparable object2) { if (object1 == null) { return false; // null is not bigger than anything } return object1.CompareTo(object2) > 0; }
public static bool IsSmaller(this IComparable object1, IComparable object2) { if (object1 == null) { return object2 != null; // smaller than anything but null } return object1.CompareTo(object2) < 0; }
/// <summary> /// Compares string builder with text, /// when it's different, Clears string builder and Appends text. /// Returns true when original string builder was modified. /// </summary> public static bool CompareUpdate(this StringBuilder sb, StringBuilder text) { if (sb.CompareTo(text) != 0) { sb.Clear(); sb.AppendStringBuilder(text); return true; } return false; }
/// <summary> /// Compara dos valores /// </summary> public static int CompareIgnoreNullTo(this string strFirst, string strSecond) { if (string.IsNullOrEmpty(strFirst) && string.IsNullOrEmpty(strSecond)) return 0; else if (string.IsNullOrEmpty(strFirst) && !string.IsNullOrEmpty(strSecond)) return -1; else if (!string.IsNullOrEmpty(strFirst) && string.IsNullOrEmpty(strSecond)) return 1; else return strFirst.CompareTo(strSecond); }
public static bool NearlyEquals(this float a, float b) { float absA = Math.Abs(a); float absB = Math.Abs(b); float diff = Math.Abs(a - b); if (a.CompareTo(b) == 0) { // shortcut, handles infinities return true; } else if (a.CompareTo(0f) == 0 || b.CompareTo(0f) == 0 || diff < float.MinValue) { // a or b is zero or both are extremely close to it // relative error is less meaningful here return diff < (float.Epsilon * float.MinValue); } else { // use relative error return diff / Math.Min((absA + absB), float.MaxValue) < float.Epsilon; } }
public static bool EnhancedCompareTo(this DateTime itemValue, Criteria criteria) { DateTime dest; bool ok = DateTime.TryParse(criteria.Value, out dest); if (!ok) return false; switch (criteria.Operator) { case Operators.Equal: return itemValue.CompareTo(dest) == 0; case Operators.NotEqual: return itemValue.CompareTo(dest) != 0; case Operators.EarlierThan: return itemValue.CompareTo(dest) < 0; case Operators.LaterThan: return itemValue.CompareTo(dest) > 0; } return false; }
/// <summary> /// 対象日が期間内かどうかのチェック /// </summary> /// <param name="date">対象日</param> /// <param name="longFrom">期間開始日</param> /// <param name="longTo">期間終了日</param> /// <returns>bool</returns> public static bool Between(this DateTime date, DateTime longFrom, DateTime longTo) { if (longTo.CompareTo(longFrom) < 0) { return false; } if (date.CompareTo(longFrom) < 0) { return false; } if (longTo.CompareTo(date) < 0) { return false; } return true; }
/// <summary>以指定的比较类型比较指定日期早于、等于或晚于当前指定的日期</summary> /// <param name="t1">当前指定的日期</param> /// <param name="t2">要比较的日期</param> /// <param name="compareType">比较类型</param> /// <returns>以 -1、0、1 表示的早于、等于或晚于当前指定的日期</returns> public static int CompareTo_(this DateTime t1, DateTime t2, DateTimeCompareType compareType = DateTimeCompareType.None) { if(compareType == DateTimeCompareType.None) return t1.CompareTo(t2); DateTime d1, d2; int y1 = t1.Year, y2 = t2.Year; switch(compareType) { case DateTimeCompareType.ByYear: d1 = new DateTime(y1, 1, 1); d2 = new DateTime(y2, 1, 1); break; case DateTimeCompareType.ByHalfYear: d1 = new DateTime(y1, t1.GetHalfYear_(), 1); d2 = new DateTime(y2, t2.GetHalfYear_(), 1); break; case DateTimeCompareType.ByQuarter: d1 = new DateTime(y1, t1.GetQuarter_(), 1); d2 = new DateTime(y2, t2.GetQuarter_(), 1); break; case DateTimeCompareType.ByMonth: d1 = new DateTime(y1, t1.Month, 1); d2 = new DateTime(y2, t2.Month, 1); break; case DateTimeCompareType.ByWeek: if(y1 != y2) return y1.CompareTo(y2); return t1.GetWeekOfYear_().CompareTo(t2.GetWeekOfYear_()); case DateTimeCompareType.ByDay: return t1.Date.CompareTo(t2.Date); case DateTimeCompareType.ByHour: d1 = new DateTime(y1, t1.Month, t1.Day, t1.Hour, 0, 0); d2 = new DateTime(y2, t2.Month, t2.Day, t2.Hour, 0, 0); break; case DateTimeCompareType.ByMinute: d1 = new DateTime(y1, t1.Month, t1.Day, t1.Hour, t1.Minute, 0); d2 = new DateTime(y2, t2.Month, t2.Day, t2.Hour, t2.Minute, 0); break; default: d1 = new DateTime(y1, t1.Month, t1.Day, t1.Hour, t1.Minute, t1.Second); d2 = new DateTime(y2, t2.Month, t2.Day, t2.Hour, t2.Minute, t2.Second); break; } return d1.CompareTo(d2); }
public static int CompareTitleStrings(this string x, string y) { var xEmpty = x.IsNullOrEmpty(); var yEmpty = y.IsNullOrEmpty(); if (xEmpty && yEmpty) return 0; if (xEmpty) return 1; if (yEmpty) return -1; var ruRe = new Regex(@"[а-я]+", RegexOptions.IgnoreCase); var xIsRu = ruRe.IsMatch(x); var yIsRu = ruRe.IsMatch(y); if ((xIsRu && yIsRu) || !(xIsRu || yIsRu)) return x.CompareTo(y); return xIsRu ? -1 : 1; }
public static TimeFrame GetTimeFrame(this DateTime date) { date = date.TrimTime(); var today = DateTime.Today; var tomorrow = today.AddDays(1); if(date.Equals(today)) return TimeFrame.Today; if(date.Equals(tomorrow)) return TimeFrame.Tomorrow; if (date.CompareTo(tomorrow) > 0) return TimeFrame.Later; return TimeFrame.Overdue; }
public static int countSpecialDays(this DateTime date, DateTime compared, bool includeWeekends, int month, int day) { DateTime earlier, later; int counter = 0; if (date.CompareTo(compared) > 0) { earlier = compared; later = date; } else { earlier = date; later = compared; } int startYear = (earlier.Month < month || earlier.Month == month && earlier.Day < day ? earlier.Year : earlier.Year + 1); int endYear = (later.Month > month || later.Month == month && later.Day >= day ? later.Year : later.Year - 1); if (includeWeekends) { return endYear - startYear + 1; } else { for (var i = startYear; i <= endYear; i++) { DateTime specialDay = new DateTime(i, month, day); if (specialDay.DayOfWeek != DayOfWeek.Sunday && specialDay.DayOfWeek != DayOfWeek.Saturday) { counter++; } } } return counter; }
public static bool CompareCurrent(this TabAwareCharacterStream stream, string text, bool ignoreCase = false) { return stream.CompareTo(stream.Position, text.Length, text, ignoreCase); }
/// <summary> /// A T extension method that check if the value is between inclusively the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between inclusively the minValue and maxValue, otherwise false.</returns> /// ### /// <typeparam name="T">Generic type parameter.</typeparam> public static bool InRange(this Int16 @this, Int16 minValue, Int16 maxValue) { return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0; }
/// <summary> /// Check if a version is lower than the other. /// </summary> public static bool IsLowerThan(this Version version1, Version version2) { return (version1.CompareTo(version2) < 0); }
/// <summary> /// Check if a version is greater than the other. /// </summary> public static bool IsGreaterThan(this Version version1, Version version2) { return (version1.CompareTo(version2) > 0); }
public static bool IsBefore(this DateTime source, DateTime other) { return source.CompareTo(other) < 0; }
public static bool IsAfter(this DateTime source, DateTime other) { return source.CompareTo(other) > 0; }
internal static bool IsHigherThan(this Version thisVersion, Version otherVersion) { return thisVersion.CompareTo(otherVersion) > 0; }
public static bool OnOrAfter(this Version first, Version other) { return first.CompareTo(other) >= 0; }
/// <summary> /// A T extension method that check if the value is between inclusively the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between inclusively the minValue and maxValue, otherwise false.</returns> /// ### /// <typeparam name="T">Generic type parameter.</typeparam> public static bool InRange(this UInt32 @this, UInt32 minValue, UInt32 maxValue) { return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0; }
/// <summary> /// A T extension method that check if the value is between inclusively the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between inclusively the minValue and maxValue, otherwise false.</returns> /// ### /// <typeparam name="T">Generic type parameter.</typeparam> public static bool InRange(this DateTime @this, DateTime minValue, DateTime maxValue) { return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0; }
public static bool IsAfterOrEqual(this TimeSpan first, TimeSpan second) => first.CompareTo(second) >= 0;