Skip to content

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>

Gets the absolute value using PostgreSQL numeric semantics.

public PgNumeric Abs()

Returns: PgNumeric

The absolute value.

Rounds toward positive infinity.

public PgNumeric Ceiling()

Returns: PgNumeric

The least integer greater than or equal to this value.

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.

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.

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.

Rounds toward negative infinity.

public PgNumeric Floor()

Returns: PgNumeric

The greatest integer less than or equal to this value.

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.

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.

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.

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.

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.

Gets a hash consistent with scale-independent numeric equality.

public override int GetHashCode()

Returns: int

The numeric hash.

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.

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.

Computes the logarithm in the specified base.

public PgNumeric Log(PgNumeric @base)

Parameters:

base — PgNumeric

The logarithm base.

Returns: PgNumeric

The logarithm.

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.

Raises this value to a numeric power.

public PgNumeric Pow(PgNumeric exponent)

Parameters:

exponent — PgNumeric

The exponent.

Returns: PgNumeric

The power.

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.

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.

Computes the PostgreSQL numeric square root.

public PgNumeric Sqrt()

Returns: PgNumeric

The square root.

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.

Converts a finite integer exactly, rejecting fractional values and nonfinite values.

public BigInteger ToBigInteger()

Returns: BigInteger

The arbitrary-precision integer.

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.

Converts to double using PostgreSQL’s precision and range rules.

public double ToDouble()

Returns: double

The floating-point approximation.

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.

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.

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.

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.

Returns culture-independent text without insignificant fractional zeroes.

public string ToNormalizedString()

Returns: string

The normalized numeric text.

Converts to single precision using PostgreSQL’s rounding, overflow, and underflow rules.

public float ToSingle()

Returns: float

The floating-point approximation.

Returns the owned PostgreSQL text, retaining display scale.

public override string ToString()

Returns: string

The numeric text.

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:

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:

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.

Gets the additive identity for generic arithmetic.

static PgNumeric AdditiveIdentity { get; }

Value: PgNumeric

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

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

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

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

Adds numeric values in PostgreSQL.

static PgNumeric operator +(PgNumeric left, PgNumeric right)

Parameters:

left — PgNumeric

right — PgNumeric

Returns: PgNumeric

Divides numeric values using PostgreSQL’s result-scale rules.

static PgNumeric operator /(PgNumeric left, PgNumeric right)

Parameters:

left — PgNumeric

right — PgNumeric

Returns: PgNumeric

static bool operator ==(PgNumeric left, PgNumeric right)

Parameters:

left — PgNumeric

right — PgNumeric

Returns: bool

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

Converts to double precision using PostgreSQL’s precision and range rules.

public static explicit operator double(PgNumeric value)

Parameters:

value — PgNumeric

Returns: double

Converts exactly to an arbitrary integer, rejecting fractional and nonfinite values.

public static explicit operator BigInteger(PgNumeric value)

Parameters:

value — PgNumeric

Returns: BigInteger

Converts to single precision using PostgreSQL’s precision and range rules.

public static explicit operator float(PgNumeric value)

Parameters:

value — PgNumeric

Returns: float

Converts from double precision using PostgreSQL’s float8-to-numeric rules.

public static explicit operator PgNumeric(double value)

Parameters:

value — double

Returns: PgNumeric

Converts from single precision using PostgreSQL’s float4-to-numeric rules.

public static explicit operator PgNumeric(float value)

Parameters:

value — float

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

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

Converts an unsigned byte exactly without backend access.

public static implicit operator PgNumeric(byte value)

Parameters:

value — byte

Returns: PgNumeric

Converts a decimal exactly, preserving scale without backend access.

public static implicit operator PgNumeric(decimal value)

Parameters:

value — decimal

Returns: PgNumeric

Converts a signed 128-bit integer exactly without backend access.

public static implicit operator PgNumeric(Int128 value)

Parameters:

value — Int128

Returns: PgNumeric

Converts a signed 16-bit integer exactly without backend access.

public static implicit operator PgNumeric(short value)

Parameters:

value — short

Returns: PgNumeric

Converts a signed 32-bit integer exactly without backend access.

public static implicit operator PgNumeric(int value)

Parameters:

value — int

Returns: PgNumeric

Converts a signed 64-bit integer exactly without backend access.

public static implicit operator PgNumeric(long value)

Parameters:

value — long

Returns: PgNumeric

Converts a native-sized signed integer exactly without backend access.

public static implicit operator PgNumeric(nint value)

Parameters:

value — nint

Returns: PgNumeric

Converts an arbitrary integer within PostgreSQL’s numeric range without backend access.

public static implicit operator PgNumeric(BigInteger value)

Parameters:

value — BigInteger

Returns: PgNumeric

Converts a signed byte exactly without backend access.

public static implicit operator PgNumeric(sbyte value)

Parameters:

value — sbyte

Returns: PgNumeric

Converts an unsigned 128-bit integer exactly without backend access.

public static implicit operator PgNumeric(UInt128 value)

Parameters:

value — UInt128

Returns: PgNumeric

Converts an unsigned 16-bit integer exactly without backend access.

public static implicit operator PgNumeric(ushort value)

Parameters:

value — ushort

Returns: PgNumeric

Converts an unsigned 32-bit integer exactly without backend access.

public static implicit operator PgNumeric(uint value)

Parameters:

value — uint

Returns: PgNumeric

Converts an unsigned 64-bit integer exactly without backend access.

public static implicit operator PgNumeric(ulong value)

Parameters:

value — ulong

Returns: PgNumeric

Converts a native-sized unsigned integer exactly without backend access.

public static implicit operator PgNumeric(nuint value)

Parameters:

value — nuint

Returns: PgNumeric

static bool operator !=(PgNumeric left, PgNumeric right)

Parameters:

left — PgNumeric

right — PgNumeric

Returns: bool

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

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

Computes PostgreSQL’s numeric remainder.

static PgNumeric operator %(PgNumeric left, PgNumeric right)

Parameters:

left — PgNumeric

right — PgNumeric

Returns: PgNumeric

Multiplies numeric values in PostgreSQL.

static PgNumeric operator *(PgNumeric left, PgNumeric right)

Parameters:

left — PgNumeric

right — PgNumeric

Returns: PgNumeric

Subtracts numeric values in PostgreSQL.

static PgNumeric operator -(PgNumeric left, PgNumeric right)

Parameters:

left — PgNumeric

right — PgNumeric

Returns: PgNumeric

Negates a numeric value in PostgreSQL.

static PgNumeric operator -(PgNumeric value)

Parameters:

value — PgNumeric

Returns: PgNumeric

Returns the value unchanged, retaining display scale.

static PgNumeric operator +(PgNumeric value)

Parameters:

value — PgNumeric

Returns: PgNumeric