Repository navigation
OnixLabs.Numerics.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) |
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.
Extension methods that operate on decimal values, including direct access to the unscaled significand and scale adjustment.
[EditorBrowsable(EditorBrowsableState.Never)]
public static class NumericsExtensions| 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. |
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.
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. |
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. |
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.
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.
Extension methods that operate on any type implementing IBinaryInteger<TSelf>, such as int, long, BigInteger, and user-defined integer types.
| 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. |
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.
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.
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. |
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. |
Extension methods that operate on any type implementing INumber<TSelf>.
| Method | Returns | Description |
|---|---|---|
IsBetween<T>(this T, T, T) |
bool |
Determines whether a value falls inclusively between a minimum and maximum. |
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.
Extension methods that convert IEEE 754 binary floating-point values to BigDecimal or NumberInfo, controlled by a ConversionMode.
| 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. |
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.
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.
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. |
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. |
Extension methods that convert Float128 and Float256 values to NumberInfo, controlled by a ConversionMode.
| 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. |
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. |
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. |
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions