Table of Contents

Class NumberUtils

Namespace
Utils.Objects
Assembly
Utils.dll

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

left object

The left numeric operand.

right object

The right numeric operand.

Returns

int

A negative value if left < right, zero if they are equal, a positive value if left > right.

Exceptions

ArgumentException

Thrown if left or right is 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

value object

The object to test.

Returns

bool

True if value is 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

value object

The object to test.

Returns

bool

True if value is 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

type Type

CLR type to test.

Returns

bool

true when the type is a native integral primitive; otherwise false.

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

type Type

CLR type to test.

Returns

bool

true when the type is a native numeric primitive; otherwise false.

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

value object

The object to test.

Returns

bool

True if value is 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

from Type

Source CLR numeric type.

to Type

Destination CLR numeric type.

Returns

bool

true when the conversion is the identity or a recognized widening; otherwise false.

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

left object

The left numeric operand.

right object

The right numeric operand.

result int

Receives < 0 if left < right, 0 if they are equal, > 0 if left > right.

Returns

bool

True if both operands were numeric and successfully compared; otherwise false.