Skip to content

OnixLabs.Numerics.BigDecimal

matthew edited this page May 27, 2026 · 2 revisions

BigDecimal Struct

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

Definition

public readonly partial struct BigDecimal :
    IFloatingPoint<BigDecimal>,
    IValueEquatable<BigDecimal>,
    IValueComparable<BigDecimal>,
    IConvertible

Remarks

BigDecimal 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:

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.

Examples

Construction and basic arithmetic

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.90

Rounding and scale management

using 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)); // 3

Equality modes and comparers

using 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)

Constructors

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.

BigDecimal(BigInteger, int, ScaleMode)

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.

BigDecimal(float, ConversionMode)

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.

BigDecimal(double, ConversionMode)

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.

BigDecimal(Float128, ConversionMode)

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.

BigDecimal(Float256, ConversionMode)

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.

BigDecimal(decimal)

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.

Fields and Static Properties

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.

NegativeOne

public static BigDecimal NegativeOne { get; }

Gets the BigDecimal value representing negative one (-1).

Zero

public static BigDecimal Zero { get; }

Gets the BigDecimal value representing zero (0).

One

public static BigDecimal One { get; }

Gets the BigDecimal value representing one (1).

Two

public static BigDecimal Two { get; }

Gets the BigDecimal value representing two (2).

Ten

public static BigDecimal Ten { get; }

Gets the BigDecimal value representing ten (10).

E

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.

Pi

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.

Tau

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.

Properties

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.

UnscaledValue

public BigInteger UnscaledValue { get; }

Gets the unscaled integer significand. The numeric value is UnscaledValue / 10^Scale.

Scale

public int Scale { get; }

Gets the number of decimal digits to the right of the decimal point. Always non-negative.

Methods

Static Methods

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&lt;char&gt;, IFormatProvider?) BigDecimal Parses a ReadOnlySpan<char> into a BigDecimal.
Parse(ReadOnlySpan&lt;char&gt;, 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&lt;char&gt;, out BigDecimal) bool Attempts to parse a ReadOnlySpan<char> into a BigDecimal.
TryParse(ReadOnlySpan&lt;char&gt;, IFormatProvider?, out BigDecimal) bool Attempts to parse a ReadOnlySpan<char> with a format provider.
TryParse(ReadOnlySpan&lt;char&gt;, NumberStyles, IFormatProvider?, out BigDecimal) bool Attempts to parse a ReadOnlySpan<char> with number styles.

Instance Methods

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&lt;char&gt;, IFormatProvider?) string Formats this instance using the specified format span and format provider.

Abs(BigDecimal)

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.


Add(BigDecimal, BigDecimal)

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.


Subtract(BigDecimal, BigDecimal)

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.


Multiply(BigDecimal, BigDecimal)

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.


Divide(BigDecimal, BigDecimal, MidpointRounding)

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.

Mod(BigDecimal, BigDecimal)

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.


Pow(BigDecimal, int, MidpointRounding)

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.

UnaryAdd(BigDecimal)

public static BigDecimal UnaryAdd(BigDecimal value)

Returns the unary plus of the specified value (identity operation).

Returns — value unchanged.


UnarySubtract(BigDecimal)

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.


Increment(BigDecimal)

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.


IncrementFraction(BigDecimal)

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.


Decrement(BigDecimal)

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.


DecrementFraction(BigDecimal)

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.


Round(BigDecimal, int, MidpointRounding)

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.

Ceiling(BigDecimal)

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.


Floor(BigDecimal)

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.


Truncate(BigDecimal)

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.


TrimTrailingZeros(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.


SetScale(BigDecimal, int, MidpointRounding)

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.

NormalizeUnscaledValues(BigDecimal, BigDecimal)

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).


Compare(BigDecimal, BigDecimal)

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.


Min(BigDecimal, BigDecimal)

public static BigDecimal Min(BigDecimal left, BigDecimal right)

Returns — The lesser of left and right.


Max(BigDecimal, BigDecimal)

public static BigDecimal Max(BigDecimal left, BigDecimal right)

Returns — The greater of left and right.


MinMax(BigDecimal, BigDecimal)

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.


MinMagnitude(BigDecimal, BigDecimal)

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.


MaxMagnitude(BigDecimal, BigDecimal)

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.


MinMaxMagnitude(BigDecimal, BigDecimal)

public static (BigDecimal MinMagnitude, BigDecimal MaxMagnitude) MinMaxMagnitude(BigDecimal left, BigDecimal right)

Returns — A tuple (MinMagnitude, MaxMagnitude) containing the values ordered by absolute magnitude.


MinScale(BigDecimal, BigDecimal)

public static int MinScale(BigDecimal left, BigDecimal right)

Returns — The smaller of left.Scale and right.Scale.


MaxScale(BigDecimal, BigDecimal)

public static int MaxScale(BigDecimal left, BigDecimal right)

Returns — The larger of left.Scale and right.Scale.


MinMaxScale(BigDecimal, BigDecimal)

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.


IsInteger(BigDecimal)

public static bool IsInteger(BigDecimal value)

Returns — true if value has no fractional component; otherwise, false.


IsEvenInteger(BigDecimal)

public static bool IsEvenInteger(BigDecimal value)

Returns — true if value is an integer and its integral part is even; otherwise, false.


IsOddInteger(BigDecimal)

public static bool IsOddInteger(BigDecimal value)

Returns — true if value is an integer and its integral part is odd; otherwise, false.


IsNegative(BigDecimal)

public static bool IsNegative(BigDecimal value)

Returns — true if value is negative; otherwise, false.


IsPositive(BigDecimal)

public static bool IsPositive(BigDecimal value)

Returns — true if value is positive (including zero); otherwise, false.


IsZero(BigDecimal)

public static bool IsZero(BigDecimal value)

Returns — true if value is zero; otherwise, false.


Equals(BigDecimal, BigDecimal)

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.


Equals(BigDecimal, BigDecimal, BigDecimalEqualityComparer)

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.


Parse(string, IFormatProvider?)

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.

Parse(string, NumberStyles, IFormatProvider?)

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.

Parse(ReadOnlySpan<char>, IFormatProvider?)

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.

Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?)

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.

TryParse(string?, out BigDecimal)

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.


TryParse(string?, IFormatProvider?, out BigDecimal)

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.


TryParse(string?, NumberStyles, IFormatProvider?, out BigDecimal)

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.


TryParse(ReadOnlySpan<char>, out BigDecimal)

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.


TryParse(ReadOnlySpan<char>, IFormatProvider?, out BigDecimal)

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.


TryParse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?, out BigDecimal)

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.


CompareTo(BigDecimal)

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.


CompareTo(object?)

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.


Equals(BigDecimal)

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.


Equals(BigDecimal, BigDecimalEqualityComparer)

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.


Equals(object?)

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.


Equals(object?, BigDecimalEqualityComparer)

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.


GetHashCode()

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.


Round(int, MidpointRounding)

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.

Ceiling()

public BigDecimal Ceiling()

Returns the smallest integral value greater than or equal to this instance.

Returns — The ceiling of this instance.


Floor()

public BigDecimal Floor()

Returns the largest integral value less than or equal to this instance.

Returns — The floor of this instance.


Truncate()

public BigDecimal Truncate()

Removes the fractional component, returning only the integral part.

Returns — The integral component of this instance.


TrimTrailingZeros()

public BigDecimal TrimTrailingZeros()

Removes trailing zeros from the fractional part of this instance.

Returns — A BigDecimal with no unnecessary trailing fractional zeros.


SetScale(int, MidpointRounding)

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.

ToNumberInfo()

public NumberInfo ToNumberInfo()

Returns the underlying NumberInfo value that backs this BigDecimal instance.

Returns — A NumberInfo representing the current value.


ToString()

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.


ToString(string?, IFormatProvider?)

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.


ToString(ReadOnlySpan<char>, IFormatProvider?)

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.

Operators

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.

operator +(BigDecimal, BigDecimal)

public static BigDecimal operator +(BigDecimal left, BigDecimal right)

Computes the sum of left and right. Delegates to Add.


operator -(BigDecimal, BigDecimal)

public static BigDecimal operator -(BigDecimal left, BigDecimal right)

Computes the difference of left minus right. Delegates to Subtract.


operator *(BigDecimal, BigDecimal)

public static BigDecimal operator *(BigDecimal left, BigDecimal right)

Computes the product of left and right. Delegates to Multiply.


operator /(BigDecimal, BigDecimal)

public static BigDecimal operator /(BigDecimal left, BigDecimal right)

Computes the quotient of left divided by right using MidpointRounding.ToEven. Delegates to Divide.


operator %(BigDecimal, BigDecimal)

public static BigDecimal operator %(BigDecimal left, BigDecimal right)

Computes the remainder of left divided by right. Delegates to Mod.


operator +(BigDecimal)

public static BigDecimal operator +(BigDecimal value)

Unary plus. Returns value unchanged. Delegates to UnaryAdd.


operator -(BigDecimal)

public static BigDecimal operator -(BigDecimal value)

Unary negation. Delegates to UnarySubtract.


operator ++(BigDecimal)

public static BigDecimal operator ++(BigDecimal value)

Increments the integral component by one. Delegates to Increment.


operator --(BigDecimal)

public static BigDecimal operator --(BigDecimal value)

Decrements the integral component by one. Delegates to Decrement.


operator ==(BigDecimal, BigDecimal)

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).


operator !=(BigDecimal, BigDecimal)

public static bool operator !=(BigDecimal left, BigDecimal right)

Determines whether left and right are not semantically equal.


operator <(BigDecimal, BigDecimal)

public static bool operator <(BigDecimal left, BigDecimal right)

Returns true if left is less than right.


operator <=(BigDecimal, BigDecimal)

public static bool operator <=(BigDecimal left, BigDecimal right)

Returns true if left is less than or equal to right.


operator >(BigDecimal, BigDecimal)

public static bool operator >(BigDecimal left, BigDecimal right)

Returns true if left is greater than right.


operator >=(BigDecimal, BigDecimal)

public static bool operator >=(BigDecimal left, BigDecimal right)

Returns true if left is greater than or equal to right.


implicit operator BigDecimal(BigInteger)

public static implicit operator BigDecimal(BigInteger value)

Widens a BigInteger to a BigDecimal with scale 0.


implicit operator BigDecimal(sbyte)

public static implicit operator BigDecimal(sbyte value)

Widens an sbyte to a BigDecimal with scale 0.


implicit operator BigDecimal(byte)

public static implicit operator BigDecimal(byte value)

Widens a byte to a BigDecimal with scale 0.


implicit operator BigDecimal(short)

public static implicit operator BigDecimal(short value)

Widens a short to a BigDecimal with scale 0.


implicit operator BigDecimal(ushort)

public static implicit operator BigDecimal(ushort value)

Widens a ushort to a BigDecimal with scale 0.


implicit operator BigDecimal(int)

public static implicit operator BigDecimal(int value)

Widens an int to a BigDecimal with scale 0.


implicit operator BigDecimal(uint)

public static implicit operator BigDecimal(uint value)

Widens a uint to a BigDecimal with scale 0.


implicit operator BigDecimal(long)

public static implicit operator BigDecimal(long value)

Widens a long to a BigDecimal with scale 0.


implicit operator BigDecimal(ulong)

public static implicit operator BigDecimal(ulong value)

Widens a ulong to a BigDecimal with scale 0.


implicit operator BigDecimal(Int128)

public static implicit operator BigDecimal(Int128 value)

Widens an Int128 to a BigDecimal with scale 0.


implicit operator BigDecimal(UInt128)

public static implicit operator BigDecimal(UInt128 value)

Widens a UInt128 to a BigDecimal with scale 0.


implicit operator BigDecimal(float)

public static implicit operator BigDecimal(float value)

Converts a float to a BigDecimal using the default ConversionMode.


implicit operator BigDecimal(double)

public static implicit operator BigDecimal(double value)

Converts a double to a BigDecimal using the default ConversionMode.


implicit operator BigDecimal(decimal)

public static implicit operator BigDecimal(decimal value)

Converts a decimal to a BigDecimal exactly.


explicit operator BigInteger(BigDecimal)

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.


explicit operator sbyte(BigDecimal)

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.

explicit operator byte(BigDecimal)

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.

explicit operator short(BigDecimal)

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.

explicit operator ushort(BigDecimal)

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.

explicit operator int(BigDecimal)

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.

explicit operator uint(BigDecimal)

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.

explicit operator long(BigDecimal)

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.

explicit operator ulong(BigDecimal)

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.

explicit operator Int128(BigDecimal)

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.

explicit operator UInt128(BigDecimal)

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.

explicit operator float(BigDecimal)

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.

explicit operator double(BigDecimal)

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.

explicit operator decimal(BigDecimal)

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.

See Also

ONIXLabs .NET Library


Clone this wiki locally