PgNumeric
Namespace: Ankus
Assembly: Ankus.Runtime.dll
Owns a full-range PostgreSQL numeric value, including display scale, NaN, and infinities. The default value is zero. Equality ignores trailing fractional zeroes and treats NaN as equal to NaN.
[JsonConverter(typeof(PgNumericConverter))]public readonly struct PgNumeric : IComparable<PgNumeric>, IAdditionOperators<PgNumeric, PgNumeric, PgNumeric>, ISubtractionOperators<PgNumeric, PgNumeric, PgNumeric>, IMultiplyOperators<PgNumeric, PgNumeric, PgNumeric>, IDivisionOperators<PgNumeric, PgNumeric, PgNumeric>, IModulusOperators<PgNumeric, PgNumeric, PgNumeric>, IUnaryNegationOperators<PgNumeric, PgNumeric>, IUnaryPlusOperators<PgNumeric, PgNumeric>, IComparisonOperators<PgNumeric, PgNumeric, bool>, IEqualityOperators<PgNumeric, PgNumeric, bool>, IAdditiveIdentity<PgNumeric, PgNumeric>, IMultiplicativeIdentity<PgNumeric, PgNumeric>, ISpanFormattable, IFormattable, IParsable<PgNumeric>, IEquatable<PgNumeric>Implements: IComparable<PgNumeric>, IAdditionOperators<PgNumeric, PgNumeric, PgNumeric>, ISubtractionOperators<PgNumeric, PgNumeric, PgNumeric>, IMultiplyOperators<PgNumeric, PgNumeric, PgNumeric>, IDivisionOperators<PgNumeric, PgNumeric, PgNumeric>, IModulusOperators<PgNumeric, PgNumeric, PgNumeric>, IUnaryNegationOperators<PgNumeric, PgNumeric>, IUnaryPlusOperators<PgNumeric, PgNumeric>, IComparisonOperators<PgNumeric, PgNumeric, bool>, IEqualityOperators<PgNumeric, PgNumeric, bool>, IAdditiveIdentity<PgNumeric, PgNumeric>, IMultiplicativeIdentity<PgNumeric, PgNumeric>, ISpanFormattable, IFormattable, IParsable<PgNumeric>, IEquatable<PgNumeric>
Methods
Section titled “Methods”Gets the absolute value using PostgreSQL numeric semantics.
public PgNumeric Abs()Returns: PgNumeric
The absolute value.
Ceiling()
Section titled “Ceiling()”Rounds toward positive infinity.
public PgNumeric Ceiling()Returns: PgNumeric
The least integer greater than or equal to this value.
CompareTo(PgNumeric)
Section titled “CompareTo(PgNumeric)”Compares with PostgreSQL’s ordering: negative infinity, finite values, positive infinity, then NaN.
public int CompareTo(PgNumeric other)Parameters:
other — PgNumeric
The other value.
Returns: int
A negative value, zero, or a positive value for lesser, equal, or greater values.
Equals(PgNumeric)
Section titled “Equals(PgNumeric)”Compares numeric values independently of display scale and backend access.
public bool Equals(PgNumeric other)Parameters:
other — PgNumeric
The other value.
Returns: bool
Whether the values compare equal, including NaN compared with NaN.
Equals(object)
Section titled “Equals(object)”public override bool Equals(object obj)Parameters:
obj — object
Returns: bool
Computes e raised to this value using PostgreSQL numeric precision.
public PgNumeric Exp()Returns: PgNumeric
The exponential.
Floor()
Section titled “Floor()”Rounds toward negative infinity.
public PgNumeric Floor()Returns: PgNumeric
The greatest integer less than or equal to this value.
FromBigInteger(BigInteger)
Section titled “FromBigInteger(BigInteger)”Converts an arbitrary-precision integer within PostgreSQL’s 131072-digit limit.
public static PgNumeric FromBigInteger(BigInteger value)Parameters:
value — BigInteger
The integer.
Returns: PgNumeric
The numeric value without a fractional part.
FromDecimal(decimal)
Section titled “FromDecimal(decimal)”Converts a decimal exactly without requiring an active backend.
public static PgNumeric FromDecimal(decimal value)Parameters:
value — decimal
The decimal, including its stored scale.
Returns: PgNumeric
The exact numeric value.
FromDouble(double)
Section titled “FromDouble(double)”Converts a floating-point value using PostgreSQL’s float8-to-numeric conversion.
public static PgNumeric FromDouble(double value)Parameters:
value — double
The floating-point value.
Returns: PgNumeric
The numeric value, with the server’s conversion precision.
FromInteger<TInteger>(TInteger)
Section titled “FromInteger<TInteger>(TInteger)”Converts any binary integer exactly without requiring a backend, including Int128 and UInt128.
public static PgNumeric FromInteger<TInteger>(TInteger value) where TInteger : IBinaryInteger<TInteger>Type parameters:
TInteger
The integer type.
Parameters:
value — TInteger
The integer value.
Returns: PgNumeric
The exact numeric with scale zero.
FromSingle(float)
Section titled “FromSingle(float)”Converts a single-precision value with PostgreSQL’s float4-to-numeric precision rules.
public static PgNumeric FromSingle(float value)Parameters:
value — float
The floating-point value.
Returns: PgNumeric
The PostgreSQL numeric approximation.
GetHashCode()
Section titled “GetHashCode()”Gets a hash consistent with scale-independent numeric equality.
public override int GetHashCode()Returns: int
The numeric hash.
GreatestCommonDivisor(PgNumeric)
Section titled “GreatestCommonDivisor(PgNumeric)”Computes the greatest common divisor with PostgreSQL numeric semantics.
public PgNumeric GreatestCommonDivisor(PgNumeric other)Parameters:
other — PgNumeric
The other value.
Returns: PgNumeric
The greatest common divisor.
LeastCommonMultiple(PgNumeric)
Section titled “LeastCommonMultiple(PgNumeric)”Computes the least common multiple with PostgreSQL numeric semantics.
public PgNumeric LeastCommonMultiple(PgNumeric other)Parameters:
other — PgNumeric
The other value.
Returns: PgNumeric
The least common multiple.
Computes the natural logarithm.
public PgNumeric Log()Returns: PgNumeric
The natural logarithm.
Log(PgNumeric)
Section titled “Log(PgNumeric)”Computes the logarithm in the specified base.
public PgNumeric Log(PgNumeric @base)Parameters:
base — PgNumeric
The logarithm base.
Returns: PgNumeric
The logarithm.
Parse(string)
Section titled “Parse(string)”Parses PostgreSQL numeric syntax on the active backend thread, including exponents and special values.
public static PgNumeric Parse(string text)Parameters:
text — string
The PostgreSQL numeric input.
Returns: PgNumeric
The parsed value with PostgreSQL’s display scale.
Pow(PgNumeric)
Section titled “Pow(PgNumeric)”Raises this value to a numeric power.
public PgNumeric Pow(PgNumeric exponent)Parameters:
exponent — PgNumeric
The exponent.
Returns: PgNumeric
The power.
Rescale(int, int)
Section titled “Rescale(int, int)”Rounds to a declared precision and scale with PostgreSQL’s numeric typmod rules.
public PgNumeric Rescale(int precision, int scale)Parameters:
precision — int
The maximum significant digits, one through 1000.
scale — int
The declared scale. Negative scales and scales above precision require PostgreSQL 15 or later.
Returns: PgNumeric
The rescaled numeric; a range error is raised if it does not fit.
Round(int)
Section titled “Round(int)”Rounds to a fractional scale, breaking ties away from zero as PostgreSQL does.
public PgNumeric Round(int scale = 0)Parameters:
scale — int
The fractional scale; negative values round to the left of the decimal point.
Returns: PgNumeric
The rounded numeric.
Sqrt()
Section titled “Sqrt()”Computes the PostgreSQL numeric square root.
public PgNumeric Sqrt()Returns: PgNumeric
The square root.
Sum(IEnumerable<PgNumeric>)
Section titled “Sum(IEnumerable<PgNumeric>)”Sums values in enumeration order with PostgreSQL arithmetic; an empty sequence yields zero.
public static PgNumeric Sum(IEnumerable<PgNumeric> values)Parameters:
values — IEnumerable<PgNumeric>
The sequence, enumerated once and disposed even when an operation fails.
Returns: PgNumeric
The sum, retaining PostgreSQL result scale and special-value semantics.
ToBigInteger()
Section titled “ToBigInteger()”Converts a finite integer exactly, rejecting fractional values and nonfinite values.
public BigInteger ToBigInteger()Returns: BigInteger
The arbitrary-precision integer.
ToDecimal()
Section titled “ToDecimal()”Converts exactly to decimal, retaining the display scale and rejecting any value or scale change.
public decimal ToDecimal()Returns: decimal
The decimal with this value and this display scale, so FromDecimal restores the same numeric.
Exceptions:
- OverflowException: The value or its display scale cannot be represented exactly as a decimal.
Like SqlDecimal, the conversion fails when decimal cannot carry the display scale, including trailing fractional zeros beyond 28 digits or beyond its 96-bit coefficient. Round, Truncate, Rescale, or a SQL cast to a smaller numeric scale reduces the scale explicitly first.
ToDouble()
Section titled “ToDouble()”Converts to double using PostgreSQL’s precision and range rules.
public double ToDouble()Returns: double
The floating-point approximation.
ToInt16()
Section titled “ToInt16()”Casts to PostgreSQL smallint, rounding ties away from zero and reporting native range errors.
public short ToInt16()Returns: short
The server-rounded integer. Use ToInteger for an exact, backend-independent conversion.
ToInt32()
Section titled “ToInt32()”Casts to PostgreSQL integer, rounding ties away from zero and reporting native range errors.
public int ToInt32()Returns: int
The server-rounded integer. Use ToInteger for an exact, backend-independent conversion.
ToInt64()
Section titled “ToInt64()”Casts to PostgreSQL bigint, rounding ties away from zero and reporting native range errors.
public long ToInt64()Returns: long
The server-rounded integer. Use ToInteger for an exact, backend-independent conversion.
ToInteger<TInteger>()
Section titled “ToInteger<TInteger>()”Converts exactly to an integer, rejecting fractional, nonfinite, and out-of-range values.
public TInteger ToInteger<TInteger>() where TInteger : IBinaryInteger<TInteger>Type parameters:
TInteger
The requested integer type.
Returns: TInteger
The exact integer. This conversion does not require a backend.
Exceptions:
- OverflowException: The value cannot be represented exactly in the requested integer type.
ToNormalizedString()
Section titled “ToNormalizedString()”Returns culture-independent text without insignificant fractional zeroes.
public string ToNormalizedString()Returns: string
The normalized numeric text.
ToSingle()
Section titled “ToSingle()”Converts to single precision using PostgreSQL’s rounding, overflow, and underflow rules.
public float ToSingle()Returns: float
The floating-point approximation.
ToString()
Section titled “ToString()”Returns the owned PostgreSQL text, retaining display scale.
public override string ToString()Returns: string
The numeric text.
ToString(string?, IFormatProvider?)
Section titled “ToString(string?, IFormatProvider?)”Formats the value as PostgreSQL’s canonical text, the same text as ToString().
public string ToString(string? format, IFormatProvider? formatProvider)Parameters:
format — string
Null or empty; PostgreSQL text has one canonical form.
formatProvider — IFormatProvider
Ignored: the text does not depend on culture.
Returns: string
The canonical text.
Exceptions:
- FormatException: A format is specified.
Truncate(int)
Section titled “Truncate(int)”Truncates toward zero to a fractional scale.
public PgNumeric Truncate(int scale = 0)Parameters:
scale — int
The fractional scale; negative values truncate to the left of the decimal point.
Returns: PgNumeric
The truncated numeric.
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
Section titled “TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)”Copies the value’s canonical PostgreSQL text into a span, as interpolated strings and other span-based formatters request it.
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)Parameters:
destination — Span<char>
The span that receives the text.
charsWritten — int
The number of characters written.
format — ReadOnlySpan<char>
Empty; PostgreSQL text has one canonical form.
provider — IFormatProvider
Ignored: the text does not depend on culture.
Returns: bool
Whether the text fit in the destination.
Exceptions:
- FormatException: A format is specified.
TryParse(string?, out PgNumeric)
Section titled “TryParse(string?, out PgNumeric)”Tries to parse numeric input on the active backend thread.
public static bool TryParse(string? text, out PgNumeric value)Parameters:
text — string
The candidate input.
value — PgNumeric
The parsed numeric, or zero on invalid input.
Returns: bool
Whether the input was valid. Backend-access and operational errors still throw.
Invalid input leaves no error pending: PostgreSQL 16 and later report it as a soft input error, and earlier majors roll back an internal subtransaction. Before PostgreSQL 16, transaction callbacks and parallel operations forbid that rollback; native errors there remain pending and throw PgException.
Properties
Section titled “Properties”AdditiveIdentity
Section titled “AdditiveIdentity”Gets the additive identity for generic arithmetic.
static PgNumeric AdditiveIdentity { get; }Value: PgNumeric
IsFinite
Section titled “IsFinite”Gets whether the value is finite.
public bool IsFinite { get; }Value: bool
Gets whether the value is PostgreSQL NaN.
public bool IsNaN { get; }Value: bool
MultiplicativeIdentity
Section titled “MultiplicativeIdentity”Gets the multiplicative identity for generic arithmetic.
static PgNumeric MultiplicativeIdentity { get; }Value: PgNumeric
Gets PostgreSQL’s not-a-number value, which sorts above all other numeric values.
public static PgNumeric NaN { get; }Value: PgNumeric
NegativeInfinity
Section titled “NegativeInfinity”Gets negative infinity. Native numeric infinities require PostgreSQL 14 or later.
public static PgNumeric NegativeInfinity { get; }Value: PgNumeric
Gets one with scale zero, without backend access.
public static PgNumeric One { get; }Value: PgNumeric
PositiveInfinity
Section titled “PositiveInfinity”Gets positive infinity. Native numeric infinities require PostgreSQL 14 or later.
public static PgNumeric PositiveInfinity { get; }Value: PgNumeric
Gets the display scale, or null for NaN and infinities.
public int? Scale { get; }Value: int?
Gets -1, 0, or 1 for negative, zero, or positive values, or null for NaN.
public int? Sign { get; }Value: int?
Gets the owned, culture-independent PostgreSQL output text, retaining display scale.
public string Text { get; }Value: string
Gets zero with scale zero, without backend access.
public static PgNumeric Zero { get; }Value: PgNumeric
Operators
Section titled “Operators”operator +(PgNumeric, PgNumeric)
Section titled “operator +(PgNumeric, PgNumeric)”Adds numeric values in PostgreSQL.
static PgNumeric operator +(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: PgNumeric
operator /(PgNumeric, PgNumeric)
Section titled “operator /(PgNumeric, PgNumeric)”Divides numeric values using PostgreSQL’s result-scale rules.
static PgNumeric operator /(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: PgNumeric
operator ==(PgNumeric, PgNumeric)
Section titled “operator ==(PgNumeric, PgNumeric)”static bool operator ==(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: bool
explicit operator decimal(PgNumeric)
Section titled “explicit operator decimal(PgNumeric)”Converts exactly to decimal, rejecting rounding, overflow, nonfinite values, and display-scale loss.
public static explicit operator decimal(PgNumeric value)Parameters:
value — PgNumeric
Returns: decimal
explicit operator double(PgNumeric)
Section titled “explicit operator double(PgNumeric)”Converts to double precision using PostgreSQL’s precision and range rules.
public static explicit operator double(PgNumeric value)Parameters:
value — PgNumeric
Returns: double
explicit operator BigInteger(PgNumeric)
Section titled “explicit operator BigInteger(PgNumeric)”Converts exactly to an arbitrary integer, rejecting fractional and nonfinite values.
public static explicit operator BigInteger(PgNumeric value)Parameters:
value — PgNumeric
Returns: BigInteger
explicit operator float(PgNumeric)
Section titled “explicit operator float(PgNumeric)”Converts to single precision using PostgreSQL’s precision and range rules.
public static explicit operator float(PgNumeric value)Parameters:
value — PgNumeric
Returns: float
explicit operator PgNumeric(double)
Section titled “explicit operator PgNumeric(double)”Converts from double precision using PostgreSQL’s float8-to-numeric rules.
public static explicit operator PgNumeric(double value)Parameters:
value — double
Returns: PgNumeric
explicit operator PgNumeric(float)
Section titled “explicit operator PgNumeric(float)”Converts from single precision using PostgreSQL’s float4-to-numeric rules.
public static explicit operator PgNumeric(float value)Parameters:
value — float
Returns: PgNumeric
operator >(PgNumeric, PgNumeric)
Section titled “operator >(PgNumeric, PgNumeric)”Tests whether the left numeric is greater than the right numeric.
static bool operator >(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: bool
operator >=(PgNumeric, PgNumeric)
Section titled “operator >=(PgNumeric, PgNumeric)”Tests whether the left numeric is greater than or equal to the right numeric.
static bool operator >=(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: bool
implicit operator PgNumeric(byte)
Section titled “implicit operator PgNumeric(byte)”Converts an unsigned byte exactly without backend access.
public static implicit operator PgNumeric(byte value)Parameters:
value — byte
Returns: PgNumeric
implicit operator PgNumeric(decimal)
Section titled “implicit operator PgNumeric(decimal)”Converts a decimal exactly, preserving scale without backend access.
public static implicit operator PgNumeric(decimal value)Parameters:
value — decimal
Returns: PgNumeric
implicit operator PgNumeric(Int128)
Section titled “implicit operator PgNumeric(Int128)”Converts a signed 128-bit integer exactly without backend access.
public static implicit operator PgNumeric(Int128 value)Parameters:
value — Int128
Returns: PgNumeric
implicit operator PgNumeric(short)
Section titled “implicit operator PgNumeric(short)”Converts a signed 16-bit integer exactly without backend access.
public static implicit operator PgNumeric(short value)Parameters:
value — short
Returns: PgNumeric
implicit operator PgNumeric(int)
Section titled “implicit operator PgNumeric(int)”Converts a signed 32-bit integer exactly without backend access.
public static implicit operator PgNumeric(int value)Parameters:
value — int
Returns: PgNumeric
implicit operator PgNumeric(long)
Section titled “implicit operator PgNumeric(long)”Converts a signed 64-bit integer exactly without backend access.
public static implicit operator PgNumeric(long value)Parameters:
value — long
Returns: PgNumeric
implicit operator PgNumeric(nint)
Section titled “implicit operator PgNumeric(nint)”Converts a native-sized signed integer exactly without backend access.
public static implicit operator PgNumeric(nint value)Parameters:
value — nint
Returns: PgNumeric
implicit operator PgNumeric(BigInteger)
Section titled “implicit operator PgNumeric(BigInteger)”Converts an arbitrary integer within PostgreSQL’s numeric range without backend access.
public static implicit operator PgNumeric(BigInteger value)Parameters:
value — BigInteger
Returns: PgNumeric
implicit operator PgNumeric(sbyte)
Section titled “implicit operator PgNumeric(sbyte)”Converts a signed byte exactly without backend access.
public static implicit operator PgNumeric(sbyte value)Parameters:
value — sbyte
Returns: PgNumeric
implicit operator PgNumeric(UInt128)
Section titled “implicit operator PgNumeric(UInt128)”Converts an unsigned 128-bit integer exactly without backend access.
public static implicit operator PgNumeric(UInt128 value)Parameters:
value — UInt128
Returns: PgNumeric
implicit operator PgNumeric(ushort)
Section titled “implicit operator PgNumeric(ushort)”Converts an unsigned 16-bit integer exactly without backend access.
public static implicit operator PgNumeric(ushort value)Parameters:
value — ushort
Returns: PgNumeric
implicit operator PgNumeric(uint)
Section titled “implicit operator PgNumeric(uint)”Converts an unsigned 32-bit integer exactly without backend access.
public static implicit operator PgNumeric(uint value)Parameters:
value — uint
Returns: PgNumeric
implicit operator PgNumeric(ulong)
Section titled “implicit operator PgNumeric(ulong)”Converts an unsigned 64-bit integer exactly without backend access.
public static implicit operator PgNumeric(ulong value)Parameters:
value — ulong
Returns: PgNumeric
implicit operator PgNumeric(nuint)
Section titled “implicit operator PgNumeric(nuint)”Converts a native-sized unsigned integer exactly without backend access.
public static implicit operator PgNumeric(nuint value)Parameters:
value — nuint
Returns: PgNumeric
operator !=(PgNumeric, PgNumeric)
Section titled “operator !=(PgNumeric, PgNumeric)”static bool operator !=(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: bool
operator <(PgNumeric, PgNumeric)
Section titled “operator <(PgNumeric, PgNumeric)”Tests whether the left numeric is less than the right numeric.
static bool operator <(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: bool
operator <=(PgNumeric, PgNumeric)
Section titled “operator <=(PgNumeric, PgNumeric)”Tests whether the left numeric is less than or equal to the right numeric.
static bool operator <=(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: bool
operator %(PgNumeric, PgNumeric)
Section titled “operator %(PgNumeric, PgNumeric)”Computes PostgreSQL’s numeric remainder.
static PgNumeric operator %(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: PgNumeric
operator *(PgNumeric, PgNumeric)
Section titled “operator *(PgNumeric, PgNumeric)”Multiplies numeric values in PostgreSQL.
static PgNumeric operator *(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: PgNumeric
operator -(PgNumeric, PgNumeric)
Section titled “operator -(PgNumeric, PgNumeric)”Subtracts numeric values in PostgreSQL.
static PgNumeric operator -(PgNumeric left, PgNumeric right)Parameters:
left — PgNumeric
right — PgNumeric
Returns: PgNumeric
operator -(PgNumeric)
Section titled “operator -(PgNumeric)”Negates a numeric value in PostgreSQL.
static PgNumeric operator -(PgNumeric value)Parameters:
value — PgNumeric
Returns: PgNumeric
operator +(PgNumeric)
Section titled “operator +(PgNumeric)”Returns the value unchanged, retaining display scale.
static PgNumeric operator +(PgNumeric value)Parameters:
value — PgNumeric
Returns: PgNumeric