Files
ww-jst/wmsjst/JSMachine.WMS.Infrastructure/Extensions/ObjectExtensions.cs
T
2026-09-02 16:31:50 +08:00

31 lines
909 B
C#

// ReSharper disable once CheckNamespace
namespace System
{
public static class ObjectExtensions
{
private const double Tolerance = 1e-6;
public static T CastTo<T>(this object value)
{
return typeof(T).IsValueType && value != null
? (T)Convert.ChangeType(value, typeof(T))
: value is T typeValue ? typeValue : default;
}
public static bool EqualsWithinTolerance(this double @this, double other)
{
return Math.Abs(@this - other) < Tolerance;
}
public static bool GreaterOrEqual(this double @this, double other)
{
return @this > other || @this.EqualsWithinTolerance(other);
}
public static bool LessOrEqual(this double @this, double other)
{
return @this < other || @this.EqualsWithinTolerance(other);
}
}
}