confluent-kafka-dotnet
Show / Hide Table of Contents

Struct BigDecimal

Arbitrary-precision signed decimal, the CEL Decimal backing type for this client and the unscaled/scale representation used by the Variant codec. A value is unscaled × 10^(-scale) — a BigInteger unscaled value and an int scale, exactly like Java's java.math.BigDecimal. .NET's decimal tops out at 28–29 significant digits; parity with the other Schema Registry clients (Java/Go/Python/ JS/Rust) requires the 38-significant-digit division and square root those clients produce, so the arithmetic here mirrors Java's MathContext(38, HALF_UP).

Implements
IComparable<BigDecimal>
IEquatable<BigDecimal>
Inherited Members
object.Equals(object, object)
object.GetType()
object.ReferenceEquals(object, object)
Namespace: Confluent.SchemaRegistry
Assembly: Confluent.SchemaRegistry.dll
Syntax
public readonly struct BigDecimal : IComparable<BigDecimal>, IEquatable<BigDecimal>

Constructors

BigDecimal(BigInteger, int)

Declaration
public BigDecimal(BigInteger unscaled, int scale)
Parameters
Type Name Description
BigInteger unscaled
int scale

Fields

SaneCoefficient

A far tighter ceiling on what can be encoded, which bounds a different resource.

Declaration
public const int SaneCoefficient = 4300
Field Value
Type Description
int
Remarks

confluent.type.Decimal.value is the unscaled integer in base 256, and decimal to binary radix conversion is quadratic in every client - here it is BigInteger.ToString(), which Digits(BigInteger) and the wire encoder both call. 4300 is CPython's own int_max_str_digits, the cap it puts on string/integer conversion for exactly this reason; the Python, C++ and JS clients all adopt it, so every client agrees on which decimals can be written. CEL's documented decimal precision is 38 digits, so this leaves two orders of headroom over anything a rule is meant to produce.

SaneWidth

The width ceiling for a computation, in decimal digits.

Declaration
public const int SaneWidth = 10000000
Field Value
Type Description
int
Remarks

Deliberately not Java's - BigInteger tops out at Integer.MAX_VALUE bits, which is 646456993 digits, and reproducing that bound across six decimal libraries is neither achievable nor the point. This is a round number chosen so no single rule evaluation can exhaust memory. Width failure is the one thing that cannot be turned into a rule error after the fact: BigInteger here is unbounded until the process dies, where Java raises an ArithmeticException. Measured on the shared libmpdec in the Python client, peak RSS on operands 1e2147483647 and 3: mul, div, comparison, negation and abs all 13 MB; add 1738 MB, sub 1738 MB, remainder 1733 MB. So the guards follow exponent alignment, not arithmetic - the operations that have to build a positional form.

Zero

Declaration
public static readonly BigDecimal Zero
Field Value
Type Description
BigDecimal

Properties

Scale

Number of fractional digits (negative means trailing integer zeros).

Declaration
public int Scale { get; }
Property Value
Type Description
int

Signum

-1, 0 or 1 as the value is negative, zero or positive.

Declaration
public int Signum { get; }
Property Value
Type Description
int

Unscaled

Unscaled two's-complement integer value.

Declaration
public BigInteger Unscaled { get; }
Property Value
Type Description
BigInteger

Methods

Abs()

Declaration
public BigDecimal Abs()
Returns
Type Description
BigDecimal

Add(BigDecimal)

Declaration
public BigDecimal Add(BigDecimal other)
Parameters
Type Name Description
BigDecimal other
Returns
Type Description
BigDecimal

CompareTo(BigDecimal)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

Declaration
public int CompareTo(BigDecimal other)
Parameters
Type Name Description
BigDecimal other

An object to compare with this instance.

Returns
Type Description
int

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes other in the sort order.
Zero This instance occurs in the same position in the sort order as other.
Greater than zero This instance follows other in the sort order.

Divide(BigDecimal)

Division at Confluent.SchemaRegistry.BigDecimal.DivisionPrecision significant digits with HALF_UP rounding — Java divide(divisor, MathContext(38, HALF_UP)). A result that terminates within the precision keeps its exact (trailing-zero-stripped) value; a non-terminating result is rounded to 38 significant digits.

Declaration
public BigDecimal Divide(BigDecimal divisor)
Parameters
Type Name Description
BigDecimal divisor
Returns
Type Description
BigDecimal

Equals(BigDecimal)

Numeric equality (ignores scale), matching decimals.eq.

Declaration
public bool Equals(BigDecimal other)
Parameters
Type Name Description
BigDecimal other
Returns
Type Description
bool

Equals(object)

Indicates whether this instance and a specified object are equal.

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
object obj

The object to compare with the current instance.

Returns
Type Description
bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

Overrides
ValueType.Equals(object)

FromBigInteger(BigInteger)

Declaration
public static BigDecimal FromBigInteger(BigInteger value)
Parameters
Type Name Description
BigInteger value
Returns
Type Description
BigDecimal

FromDecimal(decimal)

Lossless conversion from a decimal, built directly from its bits so that scale and trailing zeros are preserved (1.50m becomes scale 2).

Declaration
public static BigDecimal FromDecimal(decimal value)
Parameters
Type Name Description
decimal value
Returns
Type Description
BigDecimal

FromDouble(double)

Convert a double the way Java's BigDecimal.valueOf(double) does: via the shortest decimal string that round-trips to the same double, not the exact binary value. .NET's round-trip ("R") format is that shortest string.

Declaration
public static BigDecimal FromDouble(double value)
Parameters
Type Name Description
double value
Returns
Type Description
BigDecimal

FromFloat(float)

A BigDecimal from a float, via the shortest decimal string that round-trips to the same float — Java Float.toString. A whole-number value keeps a trailing .0 (scale 1), matching Java/Python and the other clients.

Declaration
public static BigDecimal FromFloat(float value)
Parameters
Type Name Description
float value
Returns
Type Description
BigDecimal

FromLong(long)

Declaration
public static BigDecimal FromLong(long value)
Parameters
Type Name Description
long value
Returns
Type Description
BigDecimal

GetHashCode()

Returns the hash code for this instance.

Declaration
public override int GetHashCode()
Returns
Type Description
int

A 32-bit signed integer that is the hash code for this instance.

Overrides
ValueType.GetHashCode()

Max(BigDecimal)

Declaration
public BigDecimal Max(BigDecimal other)
Parameters
Type Name Description
BigDecimal other
Returns
Type Description
BigDecimal

Min(BigDecimal)

Declaration
public BigDecimal Min(BigDecimal other)
Parameters
Type Name Description
BigDecimal other
Returns
Type Description
BigDecimal

Multiply(BigDecimal)

Declaration
public BigDecimal Multiply(BigDecimal other)
Parameters
Type Name Description
BigDecimal other
Returns
Type Description
BigDecimal

Negate()

Declaration
public BigDecimal Negate()
Returns
Type Description
BigDecimal

Parse(string)

Parse a decimal string, accepting an optional sign, an optional fractional part, and optional scientific-notation exponent — the same grammar as Java's new BigDecimal(String).

Declaration
public static BigDecimal Parse(string s)
Parameters
Type Name Description
string s
Returns
Type Description
BigDecimal

Remainder(BigDecimal)

Remainder with the sign of the dividend — Java BigDecimal.remainder, matching SQL MOD. Throws on a zero divisor.

Declaration
public BigDecimal Remainder(BigDecimal divisor)
Parameters
Type Name Description
BigDecimal divisor
Returns
Type Description
BigDecimal

RequireSaneWidth(long, string, string, int)

Refuses a positional form too wide to build.

Declaration
public static void RequireSaneWidth(long needed, string fn, string what, int limit = 10000000)
Parameters
Type Name Description
long needed
string fn
string what
int limit

SetScale(int, Rounding)

Return the value at newScale fractional digits, rounding with mode — Java setScale(newScale, mode).

Declaration
public BigDecimal SetScale(int newScale, BigDecimal.Rounding mode)
Parameters
Type Name Description
int newScale
BigDecimal.Rounding mode
Returns
Type Description
BigDecimal

Sqrt()

Square root at Confluent.SchemaRegistry.BigDecimal.DivisionPrecision significant digits, HALF_UP — Java sqrt(MathContext(38, HALF_UP)). Throws on a negative value.

Declaration
public BigDecimal Sqrt()
Returns
Type Description
BigDecimal

Subtract(BigDecimal)

Declaration
public BigDecimal Subtract(BigDecimal other)
Parameters
Type Name Description
BigDecimal other
Returns
Type Description
BigDecimal

ToDecimal()

Nearest decimal (may lose precision). Throws OverflowException when the integer part does not fit in a decimal — Java toBigDecimal narrowed to System.Decimal.

Declaration
public decimal ToDecimal()
Returns
Type Description
decimal

ToDouble()

Nearest double (may lose precision; ±Infinity out of range) — Java doubleValue.

Declaration
public double ToDouble()
Returns
Type Description
double

ToPlainString()

Plain decimal string with no scientific notation — Java toPlainString.

Declaration
public string ToPlainString()
Returns
Type Description
string

ToString()

Returns the fully qualified type name of this instance.

Declaration
public override string ToString()
Returns
Type Description
string

The fully qualified type name.

Overrides
ValueType.ToString()

UnscaledPrecision(BigInteger)

The digit count of an unscaled value, which is what BigDecimal.precision() reports and what confluent.type.Decimal.precision carries. Zero is 1 there, never 0.

Declaration
public static uint UnscaledPrecision(BigInteger unscaled)
Parameters
Type Name Description
BigInteger unscaled
Returns
Type Description
uint
Remarks

Guarded, and the single definition for every write path in this client: BigInteger.ToString() is a quadratic radix conversion, so counting the digits of an unbounded coefficient is the cost SaneCoefficient exists to bound. The estimate comes from the two's-complement byte length - ToByteArray() is needed to write the value anyway - because GetBitLength() does not exist on this project's netstandard2.0 and net462 targets.

Implements

IComparable<T>
IEquatable<T>

Extension Methods

DecimalExtensions.ToProtobufDecimal(BigDecimal)
In this article