Class NumberUtils
Provides helper methods for inspecting numeric types and comparing loosely typed values.
public static class NumberUtils
- Inheritance
-
NumberUtils
- Inherited Members
Methods
CompareNumeric(object, object)
Compares two objects that are presumed to be numeric. If both are integral, uses integer-based comparison (by converting to long). Otherwise, falls back to decimal-based comparison to preserve as much precision as possible.
public static int CompareNumeric(object left, object right)
Parameters
Returns
- int
A negative value if
left<right, zero if they are equal, a positive value ifleft>right.
Exceptions
- ArgumentException
Thrown if
leftorrightis not numeric.
IsFloatingPointType(object)
Checks whether the given value is a floating-point type,
i.e. it implements IFloatingPoint<TSelf>.
public static bool IsFloatingPointType(object value)
Parameters
valueobjectThe object to test.
Returns
- bool
True if
valueis floating-point, otherwise false.
IsIntegralType(object)
Checks whether the given value is integral,
i.e. it implements IBinaryInteger<TSelf>.
public static bool IsIntegralType(object value)
Parameters
valueobjectThe object to test.
Returns
- bool
True if
valueis integral, otherwise false.
IsNativeIntegerType(Type)
Checks whether the given CLR type is one of the native integral primitives.
This helper intentionally targets built-in integer types and is distinct from
generic math interface checks such as IBinaryInteger<TSelf>.
public static bool IsNativeIntegerType(Type type)
Parameters
typeTypeCLR type to test.
Returns
- bool
truewhen the type is a native integral primitive; otherwisefalse.
IsNativeNumericType(Type)
Checks whether the given CLR type is one of the native numeric primitives.
This helper intentionally targets built-in numeric types and is distinct from
generic math interface checks such as INumber<TSelf>.
public static bool IsNativeNumericType(Type type)
Parameters
typeTypeCLR type to test.
Returns
- bool
truewhen the type is a native numeric primitive; otherwisefalse.
IsNumeric(object)
Checks whether the given value is recognized as a numeric type
by verifying that its runtime type implements INumber<TSelf>.
public static bool IsNumeric(object value)
Parameters
valueobjectThe object to test.
Returns
- bool
True if
valueis numeric, otherwise false.
IsWideningNumericConversion(Type, Type)
Checks whether an implicit (widening) numeric conversion exists from from to
to. Unlike checking IsNativeNumericType(Type) on both endpoints,
this rejects conversions that reduce range or truncate, such as double to
float, double to int, or long to
short. It does not guarantee the conversion is lossless: some accepted entries —
matching C#'s own implicit numeric conversions (e.g. long to float) as
well as the deliberate decimal-to-floating-point extension noted on
Utils.Objects.NumberUtils.WideningNumericConversions — can still lose precision for values near the source
type's upper range or significant-digit limit.
public static bool IsWideningNumericConversion(Type from, Type to)
Parameters
Returns
- bool
truewhen the conversion is the identity or a recognized widening; otherwisefalse.
TryCompareNumeric(object, object, out int)
Tries to compare two objects as numeric values without throwing exceptions for invalid or overflow cases. If both are integral, compares them as 64-bit integers; otherwise attempts decimal-based comparison.
public static bool TryCompareNumeric(object left, object right, out int result)
Parameters
leftobjectThe left numeric operand.
rightobjectThe right numeric operand.
resultintReceives < 0 if
left<right, 0 if they are equal, > 0 ifleft>right.
Returns
- bool
True if both operands were numeric and successfully compared; otherwise false.