Repository navigation
OnixLabs.Numerics.BigDecimal
Represents an arbitrarily large signed decimal number with an arbitrary-precision significand and a fixed non-negative scale, integrating fully with .NET's generic-math infrastructure.
| Namespace | OnixLabs.Numerics |
| Assembly | OnixLabs.Numerics.dll (NuGet) |
| Inheritance |
object → ValueType → BigDecimal
|
| Implements |
IFloatingPoint<BigDecimal>, IValueEquatable<BigDecimal>, IValueComparable<BigDecimal>, IConvertible
|
public readonly partial struct BigDecimal :
IFloatingPoint<BigDecimal>,
IValueEquatable<BigDecimal>,
IValueComparable<BigDecimal>,
IConvertibleBigDecimal is an arbitrary-precision signed decimal built on top of BigInteger. Every value is stored as an unscaled integer (UnscaledValue) together with a non-negative scale that records the number of decimal digits to the right of the decimal point. For example, the value 1.23 is represented as UnscaledValue = 123, Scale = 2. Because neither component has a fixed bit-width there is no minimum or maximum representable value.
Equality — strict versus semantic. Two equality modes are available through BigDecimalEqualityComparer:
-
Strict equality — used by
Equals(BigDecimal),Equals(BigDecimal, BigDecimalEqualityComparer),GetHashCode(), andEquals(BigDecimal, BigDecimal)— requires the sameUnscaledValueand the sameScale. For example,1.20 ≠ 1.2under strict equality. -
Semantic equality — used by
operator ==andoperator !=— compares the numeric values, treating1.20and1.2as equal.
Ordering is always semantic (by numeric value) and is delegated to BigDecimalOrdinalityComparer.Default.
Constructors and conversions. BigDecimal can be constructed directly from a BigInteger significand and scale, or from any built-in floating-point or integer type. Implicit conversion operators are provided for all standard integer types (sbyte through ulong, Int128, UInt128, BigInteger), float, double, and decimal. Explicit conversion operators narrow a BigDecimal back to any of those types; integer conversions truncate the fractional part and throw OverflowException if the integral component is out of range.
Formatting and parsing. ToString accepts the standard .NET numeric format strings C, D, E, F, G, N, P, R, and X and honours the supplied IFormatProvider. Symmetric Parse and TryParse overloads accept both string and ReadOnlySpan<char> with optional NumberStyles and IFormatProvider control. The constants E, Pi, and Tau are pre-parsed using CultureInfo.InvariantCulture and are accurate to 1001 decimal digits.
Relationship to NumberInfo. Internally each BigDecimal wraps a NumberInfo value. The ToNumberInfo() method exposes this directly, enabling interoperability with the rest of the OnixLabs.Numerics formatting and parsing pipeline.
Generic-math interface contract. Because BigDecimal has no NaN, infinity, or subnormal states, the corresponding INumberBase<BigDecimal> predicate members (IsNaN, IsInfinity, IsFinite, IsNormal, IsSubnormal, IsComplexNumber, IsImaginaryNumber, IsRealNumber, IsCanonical) are implemented explicitly on the interface and always return the appropriate constant: IsFinite, IsCanonical, IsRealNumber always return true; IsNaN, IsInfinity, IsNegativeInfinity, IsPositiveInfinity, IsComplexNumber, IsImaginaryNumber, IsSubnormal always return false; IsNormal returns true for non-zero values. The IConvertible interface is also implemented explicitly; prefer the strongly typed explicit-cast operators or Convert.ChangeType when converting to primitive types.
using OnixLabs.Numerics;
// From a BigInteger unscaled value + scale
BigDecimal price = new BigDecimal(12345, scale: 2); // 123.45
BigDecimal tax = new BigDecimal(1000, scale: 3); // 1.000
// Implicit conversions from primitive types
BigDecimal a = 42; // from int
BigDecimal b = 3.14; // from double (binary representation by default)
BigDecimal c = 3.14m; // from decimal (exact)
// Arithmetic preserves scale
BigDecimal sum = price + tax; // 124.450
BigDecimal diff = price - tax; // 122.450
BigDecimal prod = price * new BigDecimal(2, 0); // 246.90
BigDecimal quot = BigDecimal.Divide(price, new BigDecimal(3, 0)); // 41.15 (rounded, scale 2)
Console.WriteLine(sum); // 124.450
Console.WriteLine(prod); // 246.90using OnixLabs.Numerics;
BigDecimal value = BigDecimal.Parse("3.14159265358979");
// Round to 4 decimal places using banker's rounding (default)
BigDecimal rounded = BigDecimal.Round(value, scale: 4); // 3.1416
// Expand the scale (adds trailing zeros)
BigDecimal expanded = BigDecimal.SetScale(value, 20);
// Remove trailing zeros
BigDecimal trimmed = BigDecimal.TrimTrailingZeros(new BigDecimal(12500, 3)); // 12.5
Console.WriteLine(BigDecimal.Floor(value)); // 3
Console.WriteLine(BigDecimal.Ceiling(value)); // 4
Console.WriteLine(BigDecimal.Truncate(value)); // 3using OnixLabs.Numerics;
BigDecimal x = new BigDecimal(120, 2); // 1.20
BigDecimal y = new BigDecimal(12, 1); // 1.2
// operator == uses semantic equality
Console.WriteLine(x == y); // True
// Equals() / GetHashCode() use strict equality
Console.WriteLine(x.Equals(y)); // False (different scale)
// Explicit semantic comparison
Console.WriteLine(BigDecimal.Equals(x, y, BigDecimalEqualityComparer.Semantic)); // True
// Ordering
Console.WriteLine(BigDecimal.Compare(x, y)); // 0 (semantically equal)| Constructor | Description |
|---|---|
BigDecimal(BigInteger, int, ScaleMode) |
Initializes a new instance from an unscaled integer value, a scale, and a scale mode. |
BigDecimal(float, ConversionMode) |
Initializes a new instance from a single-precision floating-point value. |
BigDecimal(double, ConversionMode) |
Initializes a new instance from a double-precision floating-point value. |
BigDecimal(Float128, ConversionMode) |
Initializes a new instance from a Float128 value. |
BigDecimal(Float256, ConversionMode) |
Initializes a new instance from a Float256 value. |
BigDecimal(decimal) |
Initializes a new instance from a decimal value. |
public BigDecimal(BigInteger value, int scale = 0, ScaleMode mode = default)Initializes a new instance of the BigDecimal struct from the specified unscaled integer value, scale, and scale mode.
| Parameter | Type | Description |
|---|---|---|
value |
BigInteger |
The unscaled integer value. |
scale |
int |
The number of decimal digits to the right of the decimal point. Defaults to 0. |
mode |
ScaleMode |
Determines how the value is scaled. Defaults to the default ScaleMode value. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
scale is less than zero. |
ArgumentOutOfRangeException |
mode is not a defined ScaleMode value. |
public BigDecimal(float value, ConversionMode mode = default)Initializes a new instance of the BigDecimal struct from a float value.
| Parameter | Type | Description |
|---|---|---|
value |
float |
The single-precision floating-point value to convert. |
mode |
ConversionMode |
Determines whether the value is converted from its binary or decimal representation. Defaults to the default ConversionMode value. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentOutOfRangeException |
mode is not a defined ConversionMode value. |
public BigDecimal(double value, ConversionMode mode = default)Initializes a new instance of the BigDecimal struct from a double value.
| Parameter | Type | Description |
|---|---|---|
value |
double |
The double-precision floating-point value to convert. |
mode |
ConversionMode |
Determines whether the value is converted from its binary or decimal representation. Defaults to the default ConversionMode value. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentOutOfRangeException |
mode is not a defined ConversionMode value. |
public BigDecimal(Float128 value, ConversionMode mode = default)Initializes a new instance of the BigDecimal struct from a Float128 value.
| Parameter | Type | Description |
|---|---|---|
value |
Float128 |
The 128-bit floating-point value to convert. |
mode |
ConversionMode |
Determines whether the value is converted from its binary or decimal representation. Defaults to the default ConversionMode value. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentOutOfRangeException |
mode is not a defined ConversionMode value. |
public BigDecimal(Float256 value, ConversionMode mode = default)Initializes a new instance of the BigDecimal struct from a Float256 value.
| Parameter | Type | Description |
|---|---|---|
value |
Float256 |
The 256-bit floating-point value to convert. |
mode |
ConversionMode |
Determines whether the value is converted from its binary or decimal representation. Defaults to the default ConversionMode value. |
Exceptions
| Exception | Condition |
|---|---|
ArgumentOutOfRangeException |
mode is not a defined ConversionMode value. |
public BigDecimal(decimal value)Initializes a new instance of the BigDecimal struct from a decimal value. The conversion is exact.
| Parameter | Type | Description |
|---|---|---|
value |
decimal |
The decimal value to convert. |
| Member | Type | Description |
|---|---|---|
NegativeOne |
BigDecimal |
Gets the BigDecimal value representing negative one (-1). |
Zero |
BigDecimal |
Gets the BigDecimal value representing zero (0). |
One |
BigDecimal |
Gets the BigDecimal value representing one (1). |
Two |
BigDecimal |
Gets the BigDecimal value representing two (2). |
Ten |
BigDecimal |
Gets the BigDecimal value representing ten (10). |
E |
BigDecimal |
Gets the natural logarithmic base e, accurate to 1001 decimal digits. |
Pi |
BigDecimal |
Gets the ratio π of a circle's circumference to its diameter, accurate to 1001 decimal digits. |
Tau |
BigDecimal |
Gets the number of radians in one turn τ, accurate to 1001 decimal digits. |
public static BigDecimal NegativeOne { get; }Gets the BigDecimal value representing negative one (-1).
public static BigDecimal Zero { get; }Gets the BigDecimal value representing zero (0).
public static BigDecimal One { get; }Gets the BigDecimal value representing one (1).
public static BigDecimal Two { get; }Gets the BigDecimal value representing two (2).
public static BigDecimal Ten { get; }Gets the BigDecimal value representing ten (10).
public static BigDecimal E { get; }Gets Euler's number e. This value is accurate to a precision of 1001 decimal digits and is initialised once at type load using CultureInfo.InvariantCulture.
public static BigDecimal Pi { get; }Gets the mathematical constant π (pi). This value is accurate to a precision of 1001 decimal digits and is initialised once at type load using CultureInfo.InvariantCulture.
public static BigDecimal Tau { get; }Gets the mathematical constant τ (tau, equal to 2π). This value is accurate to a precision of 1001 decimal digits and is initialised once at type load using CultureInfo.InvariantCulture.
| Property | Type | Description |
|---|---|---|
UnscaledValue |
BigInteger |
Gets the unscaled integer significand of the current value. |
Scale |
int |
Gets the number of decimal digits to the right of the decimal point. |
public BigInteger UnscaledValue { get; }Gets the unscaled integer significand. The numeric value is UnscaledValue / 10^Scale.
public int Scale { get; }Gets the number of decimal digits to the right of the decimal point. Always non-negative.
| Method | Returns | Description |
|---|---|---|
Abs(BigDecimal) |
BigDecimal |
Returns the absolute value. |
Add(BigDecimal, BigDecimal) |
BigDecimal |
Computes the sum of two values. |
Subtract(BigDecimal, BigDecimal) |
BigDecimal |
Computes the difference between two values. |
Multiply(BigDecimal, BigDecimal) |
BigDecimal |
Computes the product of two values. |
Divide(BigDecimal, BigDecimal, MidpointRounding) |
BigDecimal |
Computes the quotient of two values with optional rounding. |
Mod(BigDecimal, BigDecimal) |
BigDecimal |
Computes the remainder (modulus) of dividing one value by another. |
Pow(BigDecimal, int, MidpointRounding) |
BigDecimal |
Raises a value to an integer power. |
UnaryAdd(BigDecimal) |
BigDecimal |
Returns the unary plus of a value (identity). |
UnarySubtract(BigDecimal) |
BigDecimal |
Returns the unary negation of a value. |
Increment(BigDecimal) |
BigDecimal |
Increments the integral component by one. |
IncrementFraction(BigDecimal) |
BigDecimal |
Increments the fractional component by one unit in the last place. |
Decrement(BigDecimal) |
BigDecimal |
Decrements the integral component by one. |
DecrementFraction(BigDecimal) |
BigDecimal |
Decrements the fractional component by one unit in the last place. |
Round(BigDecimal, int, MidpointRounding) |
BigDecimal |
Rounds a value to the specified scale. |
Ceiling(BigDecimal) |
BigDecimal |
Returns the smallest integral value greater than or equal to the specified value. |
Floor(BigDecimal) |
BigDecimal |
Returns the largest integral value less than or equal to the specified value. |
Truncate(BigDecimal) |
BigDecimal |
Removes the fractional component, returning only the integral part. |
TrimTrailingZeros(BigDecimal) |
BigDecimal |
Removes trailing fractional zeros without changing the numeric value. |
SetScale(BigDecimal, int, MidpointRounding) |
BigDecimal |
Returns a copy of the value with the specified scale. |
NormalizeUnscaledValues(BigDecimal, BigDecimal) |
(BigInteger Left, BigInteger Right) |
Returns the two unscaled values aligned to the same scale. |
Compare(BigDecimal, BigDecimal) |
int |
Compares two values and returns a signed integer indicating their relative order. |
Min(BigDecimal, BigDecimal) |
BigDecimal |
Returns the lesser of two values. |
Max(BigDecimal, BigDecimal) |
BigDecimal |
Returns the greater of two values. |
MinMax(BigDecimal, BigDecimal) |
(BigDecimal Min, BigDecimal Max) |
Returns the lesser and greater of two values as a tuple. |
MinMagnitude(BigDecimal, BigDecimal) |
BigDecimal |
Returns the value with the smaller absolute magnitude. |
MaxMagnitude(BigDecimal, BigDecimal) |
BigDecimal |
Returns the value with the larger absolute magnitude. |
MinMaxMagnitude(BigDecimal, BigDecimal) |
(BigDecimal MinMagnitude, BigDecimal MaxMagnitude) |
Returns both values ordered by absolute magnitude. |
MinScale(BigDecimal, BigDecimal) |
int |
Returns the smaller of the two scales. |
MaxScale(BigDecimal, BigDecimal) |
int |
Returns the larger of the two scales. |
MinMaxScale(BigDecimal, BigDecimal) |
(int Min, int Max) |
Returns both scales ordered from smallest to largest. |
IsInteger(BigDecimal) |
bool |
Returns true if the value has no fractional part. |
IsEvenInteger(BigDecimal) |
bool |
Returns true if the value is an even integer. |
IsOddInteger(BigDecimal) |
bool |
Returns true if the value is an odd integer. |
IsNegative(BigDecimal) |
bool |
Returns true if the value is negative. |
IsPositive(BigDecimal) |
bool |
Returns true if the value is positive. |
IsZero(BigDecimal) |
bool |
Returns true if the value is zero. |
Equals(BigDecimal, BigDecimal) |
bool |
Compares two values for strict equality (same UnscaledValue and Scale). |
Equals(BigDecimal, BigDecimal, BigDecimalEqualityComparer) |
bool |
Compares two values using the specified equality comparer. |
Parse(string, IFormatProvider?) |
BigDecimal |
Parses a string into a BigDecimal. |
Parse(string, NumberStyles, IFormatProvider?) |
BigDecimal |
Parses a string into a BigDecimal using the specified number styles. |
Parse(ReadOnlySpan<char>, IFormatProvider?) |
BigDecimal |
Parses a ReadOnlySpan<char> into a BigDecimal. |
Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?) |
BigDecimal |
Parses a ReadOnlySpan<char> into a BigDecimal using the specified number styles. |
TryParse(string?, out BigDecimal) |
bool |
Attempts to parse a string into a BigDecimal. |
TryParse(string?, IFormatProvider?, out BigDecimal) |
bool |
Attempts to parse a string into a BigDecimal with a format provider. |
TryParse(string?, NumberStyles, IFormatProvider?, out BigDecimal) |
bool |
Attempts to parse a string with number styles. |
TryParse(ReadOnlySpan<char>, out BigDecimal) |
bool |
Attempts to parse a ReadOnlySpan<char> into a BigDecimal. |
TryParse(ReadOnlySpan<char>, IFormatProvider?, out BigDecimal) |
bool |
Attempts to parse a ReadOnlySpan<char> with a format provider. |
TryParse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?, out BigDecimal) |
bool |
Attempts to parse a ReadOnlySpan<char> with number styles. |
| Method | Returns | Description |
|---|---|---|
CompareTo(BigDecimal) |
int |
Compares this instance with another BigDecimal value. |
CompareTo(object?) |
int |
Compares this instance with another object. |
Equals(BigDecimal) |
bool |
Determines whether this instance is strictly equal to another BigDecimal. |
Equals(BigDecimal, BigDecimalEqualityComparer) |
bool |
Determines whether this instance is equal to another BigDecimal using the specified comparer. |
Equals(object?) |
bool |
Determines whether this instance is strictly equal to another object. |
Equals(object?, BigDecimalEqualityComparer) |
bool |
Determines whether this instance is equal to another object using the specified comparer. |
GetHashCode() |
int |
Returns a hash code based on strict equality (UnscaledValue and Scale). |
Round(int, MidpointRounding) |
BigDecimal |
Rounds this instance to the specified scale. |
Ceiling() |
BigDecimal |
Returns the smallest integral value greater than or equal to this instance. |
Floor() |
BigDecimal |
Returns the largest integral value less than or equal to this instance. |
Truncate() |
BigDecimal |
Returns the integral component of this instance. |
TrimTrailingZeros() |
BigDecimal |
Returns a copy of this instance with trailing fractional zeros removed. |
SetScale(int, MidpointRounding) |
BigDecimal |
Returns a copy of this instance with the specified scale. |
ToNumberInfo() |
NumberInfo |
Returns the underlying NumberInfo representation. |
ToString() |
string |
Formats this instance using the default "G" format and the current culture. |
ToString(string?, IFormatProvider?) |
string |
Formats this instance using the specified format string and format provider. |
ToString(ReadOnlySpan<char>, IFormatProvider?) |
string |
Formats this instance using the specified format span and format provider. |
public static BigDecimal Abs(BigDecimal value)Returns the absolute value of the specified BigDecimal.
| Parameter | Type | Description |
|---|---|---|
value |
BigDecimal |
The value from which to obtain an absolute value. |
Returns — A BigDecimal with the same Scale and non-negative UnscaledValue.
public static BigDecimal Add(BigDecimal left, BigDecimal right)Computes the sum of two BigDecimal values. The result scale is the maximum of the two operand scales.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The augend. |
right |
BigDecimal |
The addend. |
Returns — The sum of left and right.
public static BigDecimal Subtract(BigDecimal left, BigDecimal right)Computes the difference between two BigDecimal values. The result scale is the maximum of the two operand scales.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The minuend. |
right |
BigDecimal |
The subtrahend. |
Returns — The difference between left and right.
public static BigDecimal Multiply(BigDecimal left, BigDecimal right)Computes the product of two BigDecimal values. The result scale is the sum of the two operand scales.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The multiplicand. |
right |
BigDecimal |
The multiplier. |
Returns — The product of left and right.
public static BigDecimal Divide(BigDecimal left, BigDecimal right, MidpointRounding mode = default)Computes the quotient of two BigDecimal values. The result has the same scale as left. The remainder is rounded using the specified MidpointRounding mode.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The dividend. |
right |
BigDecimal |
The divisor. |
mode |
MidpointRounding |
The rounding strategy to apply to the remainder. Defaults to MidpointRounding.ToEven. |
Returns — The quotient of left divided by right, rounded using mode.
Exceptions
| Exception | Condition |
|---|---|
DivideByZeroException |
right is zero. |
ArgumentOutOfRangeException |
mode is not a defined MidpointRounding value. |
public static BigDecimal Mod(BigDecimal left, BigDecimal right)Returns the remainder after dividing left by right. The result scale is the maximum of the two operand scales.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The dividend. |
right |
BigDecimal |
The divisor. |
Returns — The remainder of left divided by right.
public static BigDecimal Pow(BigDecimal value, int exponent, MidpointRounding mode = default)Raises a BigDecimal value to an integer power. Negative exponents produce fractional results obtained by dividing 1 by the positive-exponent result, rounded using mode.
| Parameter | Type | Description |
|---|---|---|
value |
BigDecimal |
The base. |
exponent |
int |
The exponent. May be negative. |
mode |
MidpointRounding |
The rounding strategy to use when exponent is negative. Defaults to MidpointRounding.ToEven. |
Returns — value raised to the power of exponent.
Exceptions
| Exception | Condition |
|---|---|
DivideByZeroException |
value is zero and exponent is negative. |
public static BigDecimal UnaryAdd(BigDecimal value)Returns the unary plus of the specified value (identity operation).
Returns — value unchanged.
public static BigDecimal UnarySubtract(BigDecimal value)Returns the unary negation of the specified value. Preserves the scale.
Returns — A BigDecimal with the same Scale and negated UnscaledValue.
public static BigDecimal Increment(BigDecimal value)Increments the integral component of value by one, preserving the scale.
Returns — A new BigDecimal incremented by one integral unit.
public static BigDecimal IncrementFraction(BigDecimal value)Increments the fractional component of value by one unit in the last place (i.e., adds 10^-Scale).
Returns — A new BigDecimal incremented by one unit in the last fractional place.
public static BigDecimal Decrement(BigDecimal value)Decrements the integral component of value by one, preserving the scale.
Returns — A new BigDecimal decremented by one integral unit.
public static BigDecimal DecrementFraction(BigDecimal value)Decrements the fractional component of value by one unit in the last place (i.e., subtracts 10^-Scale).
Returns — A new BigDecimal decremented by one unit in the last fractional place.
public static BigDecimal Round(BigDecimal value, int scale = 0, MidpointRounding mode = default)Rounds value to the specified number of fractional digits using the specified midpoint-rounding strategy.
| Parameter | Type | Description |
|---|---|---|
value |
BigDecimal |
The value to round. |
scale |
int |
The number of decimal places to retain. Defaults to 0. |
mode |
MidpointRounding |
The rounding strategy. Defaults to MidpointRounding.ToEven. |
Returns — A BigDecimal rounded to scale decimal places.
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
scale is negative. |
ArgumentOutOfRangeException |
mode is not a defined MidpointRounding value. |
public static BigDecimal Ceiling(BigDecimal value)Returns the smallest integral value greater than or equal to value (rounds towards positive infinity).
Returns — The ceiling of value.
public static BigDecimal Floor(BigDecimal value)Returns the largest integral value less than or equal to value (rounds towards negative infinity).
Returns — The floor of value.
public static BigDecimal Truncate(BigDecimal value)Removes the fractional component of value, returning only the integer part (rounds towards zero).
Returns — The integral component of value as a BigDecimal.
public static BigDecimal TrimTrailingZeros(BigDecimal value)Removes trailing zeros from the fractional part of value without changing the numeric value. The integral part is never affected. Returns Zero when value is zero.
Returns — A BigDecimal with no unnecessary trailing fractional zeros.
public static BigDecimal SetScale(BigDecimal value, int scale, MidpointRounding mode = default)Returns a copy of value with the specified scale. If the new scale is larger, trailing zeros are appended. If the new scale is smaller, the value is rounded using mode.
| Parameter | Type | Description |
|---|---|---|
value |
BigDecimal |
The value to rescale. |
scale |
int |
The target scale. Must be non-negative. |
mode |
MidpointRounding |
The rounding strategy used when reducing the scale. Defaults to MidpointRounding.ToEven. |
Returns — A BigDecimal with the specified scale.
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
scale is negative. |
ArgumentOutOfRangeException |
mode is not a defined MidpointRounding value. |
public static (BigInteger Left, BigInteger Right) NormalizeUnscaledValues(BigDecimal left, BigDecimal right)Returns the unscaled values of left and right both aligned to the larger of the two scales by multiplying the value with the smaller scale by the appropriate power of ten. Useful when implementing arithmetic that requires the two operands to share the same scale.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The left operand. |
right |
BigDecimal |
The right operand. |
Returns — A tuple (BigInteger Left, BigInteger Right) where both values are expressed at MaxScale(left, right).
public static int Compare(BigDecimal left, BigDecimal right)Compares two BigDecimal values and returns a signed integer indicating their relative order. Comparison is purely by numeric value, regardless of scale.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The left operand. |
right |
BigDecimal |
The right operand. |
Returns — A negative integer if left < right, zero if left == right, or a positive integer if left > right.
public static BigDecimal Min(BigDecimal left, BigDecimal right)Returns — The lesser of left and right.
public static BigDecimal Max(BigDecimal left, BigDecimal right)Returns — The greater of left and right.
public static (BigDecimal Min, BigDecimal Max) MinMax(BigDecimal left, BigDecimal right)Returns — A tuple (Min, Max) containing the lesser and greater of left and right.
public static BigDecimal MinMagnitude(BigDecimal x, BigDecimal y)Returns the operand with the smaller absolute value. When the magnitudes are equal, x is returned if x is negative; otherwise y is returned.
Returns — The value with the smaller absolute magnitude.
public static BigDecimal MaxMagnitude(BigDecimal x, BigDecimal y)Returns the operand with the larger absolute value. When the magnitudes are equal, x is returned if x is positive; otherwise y is returned.
Returns — The value with the larger absolute magnitude.
public static (BigDecimal MinMagnitude, BigDecimal MaxMagnitude) MinMaxMagnitude(BigDecimal left, BigDecimal right)Returns — A tuple (MinMagnitude, MaxMagnitude) containing the values ordered by absolute magnitude.
public static int MinScale(BigDecimal left, BigDecimal right)Returns — The smaller of left.Scale and right.Scale.
public static int MaxScale(BigDecimal left, BigDecimal right)Returns — The larger of left.Scale and right.Scale.
public static (int Min, int Max) MinMaxScale(BigDecimal left, BigDecimal right)Returns — A tuple (Min, Max) containing the smaller and larger scales of the two operands.
public static bool IsInteger(BigDecimal value)Returns — true if value has no fractional component; otherwise, false.
public static bool IsEvenInteger(BigDecimal value)Returns — true if value is an integer and its integral part is even; otherwise, false.
public static bool IsOddInteger(BigDecimal value)Returns — true if value is an integer and its integral part is odd; otherwise, false.
public static bool IsNegative(BigDecimal value)Returns — true if value is negative; otherwise, false.
public static bool IsPositive(BigDecimal value)Returns — true if value is positive (including zero); otherwise, false.
public static bool IsZero(BigDecimal value)Returns — true if value is zero; otherwise, false.
public static bool Equals(BigDecimal left, BigDecimal right)Compares two BigDecimal values using BigDecimalEqualityComparer.Strict. Both the UnscaledValue and Scale must be identical.
Returns — true if the values are strictly equal; otherwise, false.
public static bool Equals(BigDecimal left, BigDecimal right, BigDecimalEqualityComparer comparer)Compares two BigDecimal values using the specified equality comparer.
| Parameter | Type | Description |
|---|---|---|
left |
BigDecimal |
The left operand. |
right |
BigDecimal |
The right operand. |
comparer |
BigDecimalEqualityComparer |
The equality comparer to use. |
Returns — true if the values are equal under comparer; otherwise, false.
public static BigDecimal Parse(string value, IFormatProvider? provider = null)Parses the string representation of a decimal number.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The string to parse. |
provider |
IFormatProvider? |
Culture-specific formatting information. Defaults to CultureInfo.CurrentCulture when null. |
Returns — A BigDecimal parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognisable decimal format. |
public static BigDecimal Parse(string value, NumberStyles style, IFormatProvider? provider = null)Parses the string representation of a decimal number using the specified number styles.
| Parameter | Type | Description |
|---|---|---|
value |
string |
The string to parse. |
style |
NumberStyles |
A bitwise combination of number styles allowed in value. |
provider |
IFormatProvider? |
Culture-specific formatting information. |
Returns — A BigDecimal parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognisable format under style. |
public static BigDecimal Parse(ReadOnlySpan<char> value, IFormatProvider? provider = null)Parses a ReadOnlySpan<char> representation of a decimal number.
Returns — A BigDecimal parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognisable decimal format. |
public static BigDecimal Parse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider = null)Parses a ReadOnlySpan<char> using the specified number styles.
Returns — A BigDecimal parsed from value.
Exceptions
| Exception | Condition |
|---|---|
FormatException |
value is not in a recognisable format under style. |
public static bool TryParse(string? value, out BigDecimal result)Attempts to parse value using CultureInfo.CurrentCulture.
Returns — true if parsing succeeded; otherwise, false. When false, result is default.
public static bool TryParse(string? value, IFormatProvider? provider, out BigDecimal result)Attempts to parse value using the specified format provider.
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(string? value, NumberStyles style, IFormatProvider? provider, out BigDecimal result)Attempts to parse value using the specified number styles and format provider.
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, out BigDecimal result)Attempts to parse a ReadOnlySpan<char> using CultureInfo.CurrentCulture.
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, IFormatProvider? provider, out BigDecimal result)Attempts to parse a ReadOnlySpan<char> using the specified format provider.
Returns — true if parsing succeeded; otherwise, false.
public static bool TryParse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider? provider, out BigDecimal result)Attempts to parse a ReadOnlySpan<char> using the specified number styles and format provider.
Returns — true if parsing succeeded; otherwise, false.
public int CompareTo(BigDecimal other)Compares the current instance with another BigDecimal value. Delegates to BigDecimalOrdinalityComparer.Default.
Returns — A negative integer if this instance precedes other, zero if they are equal in value, or a positive integer if this instance follows other.
public int CompareTo(object? obj)Compares the current instance with another object. Behaves as CompareTo(BigDecimal) when obj is a BigDecimal.
Returns — A signed integer indicating the relative order of the instances.
public bool Equals(BigDecimal other)Compares this instance with other using BigDecimalEqualityComparer.Strict. Both UnscaledValue and Scale must match.
Returns — true if strictly equal; otherwise, false.
public bool Equals(BigDecimal other, BigDecimalEqualityComparer comparer)Compares this instance with other using the specified equality comparer.
Returns — true if equal under comparer; otherwise, false.
public override bool Equals(object? obj)Determines whether this instance is strictly equal to another object. Returns true only when obj is a BigDecimal with the same UnscaledValue and Scale.
Returns — true if obj is an equal BigDecimal; otherwise, false.
public bool Equals(object? obj, BigDecimalEqualityComparer comparer)Determines whether this instance is equal to another object using the specified equality comparer.
Returns — true if obj is a BigDecimal that is equal to this instance under comparer; otherwise, false.
public override int GetHashCode()Returns a hash code for the current instance. The hash is computed using BigDecimalEqualityComparer.Strict, incorporating both UnscaledValue and Scale.
Returns — A 32-bit signed integer hash code.
public BigDecimal Round(int scale = 0, MidpointRounding mode = default)Rounds this instance to the specified scale. Delegates to Round(BigDecimal, int, MidpointRounding).
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
scale is negative. |
ArgumentOutOfRangeException |
mode is not a defined MidpointRounding value. |
public BigDecimal Ceiling()Returns the smallest integral value greater than or equal to this instance.
Returns — The ceiling of this instance.
public BigDecimal Floor()Returns the largest integral value less than or equal to this instance.
Returns — The floor of this instance.
public BigDecimal Truncate()Removes the fractional component, returning only the integral part.
Returns — The integral component of this instance.
public BigDecimal TrimTrailingZeros()Removes trailing zeros from the fractional part of this instance.
Returns — A BigDecimal with no unnecessary trailing fractional zeros.
public BigDecimal SetScale(int scale, MidpointRounding mode = default)Returns a copy of this instance with the specified scale. Delegates to SetScale(BigDecimal, int, MidpointRounding).
Exceptions
| Exception | Condition |
|---|---|
ArgumentException |
scale is negative. |
ArgumentOutOfRangeException |
mode is not a defined MidpointRounding value. |
public NumberInfo ToNumberInfo()Returns the underlying NumberInfo value that backs this BigDecimal instance.
Returns — A NumberInfo representing the current value.
public override string ToString()Formats this instance using the "G" format string and CultureInfo.CurrentCulture.
Returns — A string representation of this value in general format.
public string ToString(string? format, IFormatProvider? formatProvider = null)Formats this instance using the specified format string and format provider. Supported format specifiers are C, D, E, F, G, N, P, R, and X.
| Parameter | Type | Description |
|---|---|---|
format |
string? |
A standard numeric format string, or null to use "G". |
formatProvider |
IFormatProvider? |
Culture-specific formatting information. Defaults to CultureInfo.CurrentCulture when null. |
Returns — A formatted string representation of this value.
public string ToString(ReadOnlySpan<char> format, IFormatProvider? formatProvider = null)Formats this instance using the specified format span and format provider. Supported format specifiers are C, D, E, F, G, N, P, R, and X.
| Parameter | Type | Description |
|---|---|---|
format |
ReadOnlySpan<char> |
A standard numeric format span. |
formatProvider |
IFormatProvider? |
Culture-specific formatting information. Defaults to CultureInfo.CurrentCulture when null. |
Returns — A formatted string representation of this value.
| Operator | Description |
|---|---|
operator +(BigDecimal, BigDecimal) |
Computes the sum (delegates to Add). |
operator -(BigDecimal, BigDecimal) |
Computes the difference (delegates to Subtract). |
operator *(BigDecimal, BigDecimal) |
Computes the product (delegates to Multiply). |
operator /(BigDecimal, BigDecimal) |
Computes the quotient (delegates to Divide with MidpointRounding.ToEven). |
operator %(BigDecimal, BigDecimal) |
Computes the remainder (delegates to Mod). |
operator +(BigDecimal) |
Unary plus (delegates to UnaryAdd). |
operator -(BigDecimal) |
Unary negation (delegates to UnarySubtract). |
operator ++(BigDecimal) |
Increments the integral component (delegates to Increment). |
operator --(BigDecimal) |
Decrements the integral component (delegates to Decrement). |
operator ==(BigDecimal, BigDecimal) |
Semantic equality (delegates to Equals with BigDecimalEqualityComparer.Semantic). |
operator !=(BigDecimal, BigDecimal) |
Semantic inequality. |
operator <(BigDecimal, BigDecimal) |
Returns true if left is less than right. |
operator <=(BigDecimal, BigDecimal) |
Returns true if left is less than or equal to right. |
operator >(BigDecimal, BigDecimal) |
Returns true if left is greater than right. |
operator >=(BigDecimal, BigDecimal) |
Returns true if left is greater than or equal to right. |
implicit operator BigDecimal(BigInteger) |
Widens a BigInteger to BigDecimal. |
implicit operator BigDecimal(sbyte) |
Widens an sbyte to BigDecimal. |
implicit operator BigDecimal(byte) |
Widens a byte to BigDecimal. |
implicit operator BigDecimal(short) |
Widens a short to BigDecimal. |
implicit operator BigDecimal(ushort) |
Widens a ushort to BigDecimal. |
implicit operator BigDecimal(int) |
Widens an int to BigDecimal. |
implicit operator BigDecimal(uint) |
Widens a uint to BigDecimal. |
implicit operator BigDecimal(long) |
Widens a long to BigDecimal. |
implicit operator BigDecimal(ulong) |
Widens a ulong to BigDecimal. |
implicit operator BigDecimal(Int128) |
Widens an Int128 to BigDecimal. |
implicit operator BigDecimal(UInt128) |
Widens a UInt128 to BigDecimal. |
implicit operator BigDecimal(float) |
Converts a float to BigDecimal (default ConversionMode). |
implicit operator BigDecimal(double) |
Converts a double to BigDecimal (default ConversionMode). |
implicit operator BigDecimal(decimal) |
Converts a decimal to BigDecimal (exact). |
explicit operator BigInteger(BigDecimal) |
Extracts the integral part as a BigInteger. |
explicit operator sbyte(BigDecimal) |
Truncates to sbyte; throws OverflowException on range violation. |
explicit operator byte(BigDecimal) |
Truncates to byte; throws OverflowException on range violation. |
explicit operator short(BigDecimal) |
Truncates to short; throws OverflowException on range violation. |
explicit operator ushort(BigDecimal) |
Truncates to ushort; throws OverflowException on range violation. |
explicit operator int(BigDecimal) |
Truncates to int; throws OverflowException on range violation. |
explicit operator uint(BigDecimal) |
Truncates to uint; throws OverflowException on range violation. |
explicit operator long(BigDecimal) |
Truncates to long; throws OverflowException on range violation. |
explicit operator ulong(BigDecimal) |
Truncates to ulong; throws OverflowException on range violation. |
explicit operator Int128(BigDecimal) |
Truncates to Int128; throws OverflowException on range violation. |
explicit operator UInt128(BigDecimal) |
Truncates to UInt128; throws OverflowException on range violation. |
explicit operator float(BigDecimal) |
Converts to float; throws OverflowException on range violation. |
explicit operator double(BigDecimal) |
Converts to double; throws OverflowException on range violation. |
explicit operator decimal(BigDecimal) |
Converts to decimal; throws OverflowException on range violation. |
public static BigDecimal operator +(BigDecimal left, BigDecimal right)Computes the sum of left and right. Delegates to Add.
public static BigDecimal operator -(BigDecimal left, BigDecimal right)Computes the difference of left minus right. Delegates to Subtract.
public static BigDecimal operator *(BigDecimal left, BigDecimal right)Computes the product of left and right. Delegates to Multiply.
public static BigDecimal operator /(BigDecimal left, BigDecimal right)Computes the quotient of left divided by right using MidpointRounding.ToEven. Delegates to Divide.
public static BigDecimal operator %(BigDecimal left, BigDecimal right)Computes the remainder of left divided by right. Delegates to Mod.
public static BigDecimal operator +(BigDecimal value)Unary plus. Returns value unchanged. Delegates to UnaryAdd.
public static BigDecimal operator -(BigDecimal value)Unary negation. Delegates to UnarySubtract.
public static BigDecimal operator ++(BigDecimal value)Increments the integral component by one. Delegates to Increment.
public static BigDecimal operator --(BigDecimal value)Decrements the integral component by one. Delegates to Decrement.
public static bool operator ==(BigDecimal left, BigDecimal right)Determines whether left and right are semantically equal (same numeric value, regardless of scale). Delegates to Equals(left, right, BigDecimalEqualityComparer.Semantic).
public static bool operator !=(BigDecimal left, BigDecimal right)Determines whether left and right are not semantically equal.
public static bool operator <(BigDecimal left, BigDecimal right)Returns true if left is less than right.
public static bool operator <=(BigDecimal left, BigDecimal right)Returns true if left is less than or equal to right.
public static bool operator >(BigDecimal left, BigDecimal right)Returns true if left is greater than right.
public static bool operator >=(BigDecimal left, BigDecimal right)Returns true if left is greater than or equal to right.
public static implicit operator BigDecimal(BigInteger value)Widens a BigInteger to a BigDecimal with scale 0.
public static implicit operator BigDecimal(sbyte value)Widens an sbyte to a BigDecimal with scale 0.
public static implicit operator BigDecimal(byte value)Widens a byte to a BigDecimal with scale 0.
public static implicit operator BigDecimal(short value)Widens a short to a BigDecimal with scale 0.
public static implicit operator BigDecimal(ushort value)Widens a ushort to a BigDecimal with scale 0.
public static implicit operator BigDecimal(int value)Widens an int to a BigDecimal with scale 0.
public static implicit operator BigDecimal(uint value)Widens a uint to a BigDecimal with scale 0.
public static implicit operator BigDecimal(long value)Widens a long to a BigDecimal with scale 0.
public static implicit operator BigDecimal(ulong value)Widens a ulong to a BigDecimal with scale 0.
public static implicit operator BigDecimal(Int128 value)Widens an Int128 to a BigDecimal with scale 0.
public static implicit operator BigDecimal(UInt128 value)Widens a UInt128 to a BigDecimal with scale 0.
public static implicit operator BigDecimal(float value)Converts a float to a BigDecimal using the default ConversionMode.
public static implicit operator BigDecimal(double value)Converts a double to a BigDecimal using the default ConversionMode.
public static implicit operator BigDecimal(decimal value)Converts a decimal to a BigDecimal exactly.
public static explicit operator BigInteger(BigDecimal value)Extracts the integral part of value as a BigInteger. The fractional component is discarded. This conversion never throws.
public static explicit operator sbyte(BigDecimal value)Truncates the fractional part and converts the integral component to sbyte.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of sbyte. |
public static explicit operator byte(BigDecimal value)Truncates and converts to byte.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of byte. |
public static explicit operator short(BigDecimal value)Truncates and converts to short.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of short. |
public static explicit operator ushort(BigDecimal value)Truncates and converts to ushort.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of ushort. |
public static explicit operator int(BigDecimal value)Truncates and converts to int.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of int. |
public static explicit operator uint(BigDecimal value)Truncates and converts to uint.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of uint. |
public static explicit operator long(BigDecimal value)Truncates and converts to long.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of long. |
public static explicit operator ulong(BigDecimal value)Truncates and converts to ulong.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of ulong. |
public static explicit operator Int128(BigDecimal value)Truncates and converts to Int128.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of Int128. |
public static explicit operator UInt128(BigDecimal value)Truncates and converts to UInt128.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The integral component is outside the range of UInt128. |
public static explicit operator float(BigDecimal value)Converts a BigDecimal to float via a round-trip through "E" format with CultureInfo.InvariantCulture.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The value is outside the range of float. |
public static explicit operator double(BigDecimal value)Converts a BigDecimal to double via a round-trip through "E" format with CultureInfo.InvariantCulture.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The value is outside the range of double. |
public static explicit operator decimal(BigDecimal value)Converts a BigDecimal to decimal via a round-trip through "E" format with CultureInfo.InvariantCulture.
Exceptions
| Exception | Condition |
|---|---|
OverflowException |
The value is outside the range of decimal. |
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