Skip to content

OnixLabs.Numerics.NumericsExtensions

matthew edited this page May 26, 2026 · 1 revision

NumericsExtensions

Extension methods for numeric types, covering decimal scale manipulation, range testing, and conversions to BigDecimal, BigInteger, decimal, and NumberInfo.

Namespace OnixLabs.Numerics
Assembly OnixLabs.Numerics.dll (NuGet)

Remarks

NumericsExtensions is a public static extension container decorated with [EditorBrowsable(EditorBrowsableState.Never)], which keeps the class itself out of IntelliSense lists while its extension methods remain fully usable on their receiver types. Bring them into scope with using OnixLabs.Numerics;.

The methods are grouped below by the receiver type they extend.


decimal extensions

Extension methods that operate on decimal values, including direct access to the unscaled significand and scale adjustment.

[EditorBrowsable(EditorBrowsableState.Never)]
public static class NumericsExtensions

Methods

Method Returns Description
GetUnscaledValue(this decimal) BigInteger Gets the unscaled integer significand of a decimal.
SetScale(this decimal, int) decimal Sets the scale of a decimal, throwing if precision would be lost.
SetScale(this decimal, int, MidpointRounding) decimal Sets the scale of a decimal, rounding away any precision loss.
ToBigDecimal(this decimal) BigDecimal Converts a decimal to a BigDecimal.
ToNumberInfo(this decimal) NumberInfo Converts a decimal to a NumberInfo.

GetUnscaledValue(this decimal)

public static BigInteger GetUnscaledValue(this decimal value)

Gets the unscaled integer significand of the current decimal value. The result is the 96-bit significand extracted from the decimal's binary representation, negated when the decimal is negative.

Parameter Type Description
value decimal The decimal from which to obtain the unscaled value.

Returns — The unscaled significand of value as a BigInteger. For example, the unscaled value of 1.23m is 123.

SetScale(this decimal, int)

public static decimal SetScale(this decimal value, int scale)

Sets the scale (number of digits after the decimal point) of the current decimal value.

If the current scale is less than scale, the significand is padded with trailing zeros. If the current scale is greater than scale, the trailing digits are dropped — but only when no precision is lost; otherwise an InvalidOperationException is thrown. Use the SetScale(this decimal, int, MidpointRounding) overload when rounding is acceptable.

Parameter Type Description
value decimal The decimal value to adjust.
scale int The desired scale. Must be in the inclusive range 0 to 28.

Returns — A new decimal with exactly the specified scale.

Exceptions

Exception Condition
ArgumentOutOfRangeException scale is outside the inclusive range 0 to 28.
ArgumentException The required scale exceeds the maximum representable scale for the value's 96-bit significand.
InvalidOperationException Reducing the scale would drop non-zero digits, causing a loss of precision.

SetScale(this decimal, int, MidpointRounding)

public static decimal SetScale(this decimal value, int scale, MidpointRounding mode)

Sets the scale (number of digits after the decimal point) of the current decimal value, rounding away any precision loss using the specified MidpointRounding strategy.

If the current scale is less than scale, the significand is padded with trailing zeros. If the current scale is greater than scale and the dropped digits are non-zero, decimal.Round is applied with mode.

Parameter Type Description
value decimal The decimal value to adjust.
scale int The desired scale. Must be in the inclusive range 0 to 28.
mode MidpointRounding The rounding strategy to apply when reducing the scale causes precision loss.

Returns — A new decimal with exactly the specified scale.

Exceptions

Exception Condition
ArgumentOutOfRangeException scale is outside the inclusive range 0 to 28.
ArgumentException The required scale exceeds the maximum representable scale for the value's 96-bit significand.

ToBigDecimal(this decimal)

public static BigDecimal ToBigDecimal(this decimal value)

Converts the current decimal value to a BigDecimal.

Parameter Type Description
value decimal The decimal value to convert.

Returns — A new BigDecimal representing value.

ToNumberInfo(this decimal)

public static NumberInfo ToNumberInfo(this decimal value)

Converts the current decimal value to a NumberInfo value. The unscaled significand and scale are read directly from the decimal's binary representation.

Parameter Type Description
value decimal The decimal value to convert.

Returns — A new NumberInfo representing value.


IBinaryInteger<T> extensions

Extension methods that operate on any type implementing IBinaryInteger<TSelf>, such as int, long, BigInteger, and user-defined integer types.

Methods

Method Returns Description
ToBigDecimal<T>(this T, int, ScaleMode) BigDecimal Converts an integer value to a BigDecimal with the specified scale.
ToBigInteger<T>(this T) BigInteger Converts an integer value to a BigInteger.
ToDecimal<T>(this T, int, ScaleMode) decimal Converts an integer value to a decimal with the specified scale.
ToNumberInfo<T>(this T, int, ScaleMode) NumberInfo Converts an integer value to a NumberInfo with the specified scale.

ToBigDecimal<T>(this T, int, ScaleMode)

public static BigDecimal ToBigDecimal<T>(this T value, int scale = 0, ScaleMode mode = ScaleMode.Integral)
    where T : IBinaryInteger<T>

Converts the current IBinaryInteger<TSelf> value to a BigDecimal with the specified scale, using the specified ScaleMode to determine how the scale is applied.

Parameter Type Description
value T The integer value to convert.
scale int The scale of the resulting BigDecimal. Defaults to 0.
mode ScaleMode The scale mode that determines how value is scaled. Defaults to ScaleMode.Integral.
Type Parameter Description
T The underlying IBinaryInteger<TSelf> type.

Returns — A new BigDecimal representing value.

ToBigInteger<T>(this T)

public static BigInteger ToBigInteger<T>(this T value) where T : IBinaryInteger<T>

Converts the current IBinaryInteger<TSelf> value to a BigInteger via BigInteger.CreateChecked.

Parameter Type Description
value T The integer value to convert.
Type Parameter Description
T The underlying IBinaryInteger<TSelf> type.

Returns — A BigInteger representing value.

ToDecimal<T>(this T, int, ScaleMode)

public static decimal ToDecimal<T>(this T value, int scale = 0, ScaleMode mode = default)
    where T : IBinaryInteger<T>

Converts the current IBinaryInteger<TSelf> value to a decimal with the specified scale, applying the specified ScaleMode.

Parameter Type Description
value T The integer value to convert.
scale int The scale of the resulting decimal. Must be in the inclusive range 0 to 28. Defaults to 0.
mode ScaleMode The scale mode that determines how value is scaled. Defaults to ScaleMode.Fractional.
Type Parameter Description
T The underlying IBinaryInteger<TSelf> type.

Returns — A new decimal representing value at the specified scale.

Exceptions

Exception Condition
ArgumentException scale is outside the inclusive range 0 to 28.
ArgumentOutOfRangeException mode is not a defined ScaleMode value.
InvalidOperationException The scaled value falls outside the representable range of decimal.

ToNumberInfo<T>(this T, int, ScaleMode)

public static NumberInfo ToNumberInfo<T>(this T value, int scale = 0, ScaleMode mode = default)
    where T : IBinaryInteger<T>

Converts the current IBinaryInteger<TSelf> value to a NumberInfo value with the specified scale, applying the specified ScaleMode.

Parameter Type Description
value T The integer value to convert.
scale int The desired scale of the result. Must be greater than or equal to zero. Defaults to 0.
mode ScaleMode The scale mode that determines how value is scaled. Defaults to ScaleMode.Fractional.
Type Parameter Description
T The underlying IBinaryInteger<TSelf> type.

Returns — A new NumberInfo representing value.

Exceptions

Exception Condition
ArgumentException scale is less than zero.
ArgumentOutOfRangeException mode is not a defined ScaleMode value.

INumber<T> extensions

Extension methods that operate on any type implementing INumber<TSelf>.

Methods

Method Returns Description
IsBetween<T>(this T, T, T) bool Determines whether a value falls inclusively between a minimum and maximum.

IsBetween<T>(this T, T, T)

public static bool IsBetween<T>(this T value, T minimum, T maximum) where T : INumber<T>

Determines whether the current value is inclusively between the specified minimum and maximum values.

Parameter Type Description
value T The value to check.
minimum T The inclusive minimum value.
maximum T The inclusive maximum value.
Type Parameter Description
T The underlying INumber<TSelf> type.

Returns — true if value is greater than or equal to minimum and less than or equal to maximum; otherwise, false.


float and double extensions

Extension methods that convert IEEE 754 binary floating-point values to BigDecimal or NumberInfo, controlled by a ConversionMode.

Methods

Method Returns Description
ToBigDecimal(this float, ConversionMode) BigDecimal Converts a float to a BigDecimal.
ToBigDecimal(this double, ConversionMode) BigDecimal Converts a double to a BigDecimal.
ToNumberInfo(this float, ConversionMode) NumberInfo Converts a float to a NumberInfo.
ToNumberInfo(this double, ConversionMode) NumberInfo Converts a double to a NumberInfo.

ToBigDecimal(this float, ConversionMode)

public static BigDecimal ToBigDecimal(this float value, ConversionMode mode = default)

Converts the current float value to a BigDecimal using the specified ConversionMode.

Parameter Type Description
value float The float value to convert.
mode ConversionMode Specifies whether to convert from the decimal or binary representation of value. Defaults to ConversionMode.Decimal.

Returns — A new BigDecimal representing value.

ToBigDecimal(this double, ConversionMode)

public static BigDecimal ToBigDecimal(this double value, ConversionMode mode = default)

Converts the current double value to a BigDecimal using the specified ConversionMode.

Parameter Type Description
value double The double value to convert.
mode ConversionMode Specifies whether to convert from the decimal or binary representation of value. Defaults to ConversionMode.Decimal.

Returns — A new BigDecimal representing value.

ToNumberInfo(this float, ConversionMode)

public static NumberInfo ToNumberInfo(this float value, ConversionMode mode = default)

Converts the current float value to a NumberInfo using the specified ConversionMode.

Parameter Type Description
value float The float value to convert.
mode ConversionMode Specifies whether to convert from the decimal or binary representation of value. Defaults to ConversionMode.Decimal.

Returns — A new NumberInfo representing value.

Exceptions

Exception Condition
ArgumentOutOfRangeException mode is not a defined ConversionMode value.

ToNumberInfo(this double, ConversionMode)

public static NumberInfo ToNumberInfo(this double value, ConversionMode mode = default)

Converts the current double value to a NumberInfo using the specified ConversionMode.

Parameter Type Description
value double The double value to convert.
mode ConversionMode Specifies whether to convert from the decimal or binary representation of value. Defaults to ConversionMode.Decimal.

Returns — A new NumberInfo representing value.

Exceptions

Exception Condition
ArgumentOutOfRangeException mode is not a defined ConversionMode value.

Float128 and Float256 extensions

Extension methods that convert Float128 and Float256 values to NumberInfo, controlled by a ConversionMode.

Methods

Method Returns Description
ToNumberInfo(this Float128, ConversionMode) NumberInfo Converts a Float128 to a NumberInfo.
ToNumberInfo(this Float256, ConversionMode) NumberInfo Converts a Float256 to a NumberInfo.

ToNumberInfo(this Float128, ConversionMode)

public static NumberInfo ToNumberInfo(this Float128 value, ConversionMode mode = default)

Converts the current Float128 value to a NumberInfo using the specified ConversionMode.

Parameter Type Description
value Float128 The Float128 value to convert.
mode ConversionMode Specifies whether to convert from the decimal or binary representation of value. Defaults to ConversionMode.Decimal.

Returns — A new NumberInfo representing value.

Exceptions

Exception Condition
ArgumentOutOfRangeException mode is not a defined ConversionMode value.

ToNumberInfo(this Float256, ConversionMode)

public static NumberInfo ToNumberInfo(this Float256 value, ConversionMode mode = default)

Converts the current Float256 value to a NumberInfo using the specified ConversionMode.

Parameter Type Description
value Float256 The Float256 value to convert.
mode ConversionMode Specifies whether to convert from the decimal or binary representation of value. Defaults to ConversionMode.Decimal.

Returns — A new NumberInfo representing value.

Exceptions

Exception Condition
ArgumentOutOfRangeException mode is not a defined ConversionMode value.

See Also

ONIXLabs .NET Library


Clone this wiki locally