Show / Hide Table of Contents

Struct XrplNumber

A value of the XRPL Number type, used by the Single Asset Vault (XLS-65) and Lending Protocol (XLS-66) fields such as AssetsTotal, PrincipalOutstanding or PeriodicPayment.

Namespace: Xrpl.BinaryCodec.Numbers
Assembly: Xrpl.BinaryCodec.dll
Syntax
[JsonConverter(typeof(XrplNumberJsonConverter))]
public readonly struct XrplNumber
Remarks

The value is held the way rippled's Number class (include/xrpl/basics/Number.h) holds it on the large mantissa scale: a sign, a mantissa in [10^18, 10^19 - 1] and an exponent in [MinExponent, MaxExponent]. A mantissa above 9223372036854775807 is always a multiple of ten, so that the value still fits the signed 64-bit wire form reported by Mantissa and Exponent.

Parsing and conversions are exact: they refuse input that the ledger cannot hold without rounding it, the way rippled refuses such a value in JSON with "number cannot be represented", instead of signing a value other than the one written.

Arithmetic - Add(XrplNumber, XrplNumber, NumberContext), Multiply(XrplNumber, XrplNumber, NumberContext), Divide(XrplNumber, XrplNumber, NumberContext), Power(XrplNumber, uint, NumberContext), Root2(XrplNumber, NumberContext) and the rest - rounds the way rippled's Number does, bit for bit, under the NumberContext it is given: the mantissa scale the node's amendments select and the rounding mode. The operators use Default. On Small a result below about 10^-32750, which rippled can hold with its 16-digit mantissa, reads as zero here.

ToString() produces the text rippled produces for the field, switching to scientific notation (1e13) for very large and very small values.

Constructors

| Edit this page View Source

XrplNumber(long, int)

Creates a value from the wire form: mantissa × 10^exponent. The pair does not have to be normalized. As in rippled, a value too small for the exponent range becomes zero.

Declaration
public XrplNumber(long mantissa, int exponent)
Parameters
Type Name Description
long mantissa
int exponent
Exceptions
Type Condition
OverflowException

The value is too large for the exponent range, or mantissa is long.MinValue, which rippled cannot hold without rounding.

Fields

| Edit this page View Source

MaxExponent

Largest exponent of a normalized value.

Declaration
public const int MaxExponent = 32768
Field Value
Type Description
int
| Edit this page View Source

MinExponent

Smallest exponent of a normalized value.

Declaration
public const int MinExponent = -32768
Field Value
Type Description
int
| Edit this page View Source

Zero

The value zero.

Declaration
public static readonly XrplNumber Zero
Field Value
Type Description
XrplNumber

Properties

| Edit this page View Source

Exponent

Exponent of the wire form, int.MinValue for zero. Matches rippled's Number::exponent().

Declaration
public int Exponent { get; }
Property Value
Type Description
int
| Edit this page View Source

IsNegative

Whether the value is below zero.

Declaration
public bool IsNegative { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsZero

Whether the value is zero.

Declaration
public bool IsZero { get; }
Property Value
Type Description
bool
| Edit this page View Source

Mantissa

Mantissa of the wire form: signed, at most 9223372036854775807 in absolute value, 0 for zero. Matches rippled's Number::mantissa().

Declaration
public long Mantissa { get; }
Property Value
Type Description
long
| Edit this page View Source

Sign

-1, 0 or 1 by the sign of the value.

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

Methods

| Edit this page View Source

Abs(XrplNumber)

The absolute value.

Declaration
public static XrplNumber Abs(XrplNumber x)
Parameters
Type Name Description
XrplNumber x
Returns
Type Description
XrplNumber
| Edit this page View Source

Add(XrplNumber, XrplNumber, NumberContext)

x + y, rounded under context.

Declaration
public static XrplNumber Add(XrplNumber x, XrplNumber y, NumberContext context)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
NumberContext context
Returns
Type Description
XrplNumber
Exceptions
Type Condition
OverflowException

The result is beyond the exponent range.

| Edit this page View Source

CompareTo(object)

Declaration
public int CompareTo(object obj)
Parameters
Type Name Description
object obj
Returns
Type Description
int
| Edit this page View Source

CompareTo(XrplNumber)

Declaration
public int CompareTo(XrplNumber other)
Parameters
Type Name Description
XrplNumber other
Returns
Type Description
int
| Edit this page View Source

Divide(XrplNumber, XrplNumber, NumberContext)

x / y, rounded under context.

Declaration
public static XrplNumber Divide(XrplNumber x, XrplNumber y, NumberContext context)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
NumberContext context
Returns
Type Description
XrplNumber
Exceptions
Type Condition
DivideByZeroException

y is zero.

OverflowException

The result is beyond the exponent range.

| Edit this page View Source

Equals(object)

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
object obj
Returns
Type Description
bool
Overrides
System.ValueType.Equals(object)
| Edit this page View Source

Equals(XrplNumber)

Declaration
public bool Equals(XrplNumber other)
Parameters
Type Name Description
XrplNumber other
Returns
Type Description
bool
| Edit this page View Source

GetHashCode()

Declaration
public override int GetHashCode()
Returns
Type Description
int
Overrides
System.ValueType.GetHashCode()
| Edit this page View Source

Multiply(XrplNumber, XrplNumber, NumberContext)

x * y, rounded under context.

Declaration
public static XrplNumber Multiply(XrplNumber x, XrplNumber y, NumberContext context)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
NumberContext context
Returns
Type Description
XrplNumber
Exceptions
Type Condition
OverflowException

The result is beyond the exponent range.

| Edit this page View Source

Parse(string)

Parses a decimal or scientific number: "1000", "-0.025", "1e13".

Declaration
public static XrplNumber Parse(string text)
Parameters
Type Name Description
string text
Returns
Type Description
XrplNumber
Exceptions
Type Condition
FormatException

The text is not a number, has more significant digits than the ledger holds exactly, or is out of the exponent range.

| Edit this page View Source

Power(XrplNumber, uint, NumberContext)

x^n by repeated squaring, rounded at each step as rippled's power.

Declaration
public static XrplNumber Power(XrplNumber x, uint n, NumberContext context)
Parameters
Type Name Description
XrplNumber x
uint n
NumberContext context
Returns
Type Description
XrplNumber
Exceptions
Type Condition
OverflowException

The result is beyond the exponent range.

| Edit this page View Source

Root2(XrplNumber, NumberContext)

The square root of x, as rippled's root2.

Declaration
public static XrplNumber Root2(XrplNumber x, NumberContext context)
Parameters
Type Name Description
XrplNumber x
NumberContext context
Returns
Type Description
XrplNumber
Exceptions
Type Condition
OverflowException

x is negative.

ArithmeticException

The root does not converge: rippled's Newton-Raphson loop would cycle forever on this input.

| Edit this page View Source

Subtract(XrplNumber, XrplNumber, NumberContext)

x - y, rounded under context.

Declaration
public static XrplNumber Subtract(XrplNumber x, XrplNumber y, NumberContext context)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
NumberContext context
Returns
Type Description
XrplNumber
Exceptions
Type Condition
OverflowException

The result is beyond the exponent range.

| Edit this page View Source

ToInt64(NumberContext)

The value as an integer, rounded in the context's mode, as rippled's conversion to std::int64_t.

Declaration
public long ToInt64(NumberContext context)
Parameters
Type Name Description
NumberContext context
Returns
Type Description
long
Exceptions
Type Condition
OverflowException

The value is beyond long.

| Edit this page View Source

ToString()

The value as rippled writes it in JSON (to_string(Number)).

Declaration
public override string ToString()
Returns
Type Description
string
Overrides
System.ValueType.ToString()
| Edit this page View Source

Truncate(NumberContext)

The value with its fraction dropped, as rippled's truncate.

Declaration
public XrplNumber Truncate(NumberContext context)
Parameters
Type Name Description
NumberContext context
Returns
Type Description
XrplNumber
| Edit this page View Source

TryParse(string, out XrplNumber)

Parses a number the way Parse(string) does, returning false instead of throwing.

Declaration
public static bool TryParse(string text, out XrplNumber value)
Parameters
Type Name Description
string text
XrplNumber value
Returns
Type Description
bool
| Edit this page View Source

TryToDecimal(out decimal)

Converts to decimal. A value with more fractional digits than decimal holds (28) is rounded to nearest, ties to even; a value too small for that scale becomes zero.

Declaration
public bool TryToDecimal(out decimal value)
Parameters
Type Name Description
decimal value
Returns
Type Description
bool

false when the value is beyond the range of decimal.

Operators

| Edit this page View Source

operator +(XrplNumber, XrplNumber)

x + y under Default.

Declaration
public static XrplNumber operator +(XrplNumber x, XrplNumber y)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
Returns
Type Description
XrplNumber
| Edit this page View Source

operator /(XrplNumber, XrplNumber)

x / y under Default.

Declaration
public static XrplNumber operator /(XrplNumber x, XrplNumber y)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
Returns
Type Description
XrplNumber
| Edit this page View Source

operator ==(XrplNumber, XrplNumber)

Equality by value.

Declaration
public static bool operator ==(XrplNumber left, XrplNumber right)
Parameters
Type Name Description
XrplNumber left
XrplNumber right
Returns
Type Description
bool
| Edit this page View Source

explicit operator XrplNumber(decimal)

Converts from decimal.

Declaration
public static explicit operator XrplNumber(decimal value)
Parameters
Type Name Description
decimal value
Returns
Type Description
XrplNumber
Exceptions
Type Condition
OverflowException

The value has more significant digits than the ledger holds exactly; round it first.

| Edit this page View Source

explicit operator XrplNumber(long)

Converts from long.

Declaration
public static explicit operator XrplNumber(long value)
Parameters
Type Name Description
long value
Returns
Type Description
XrplNumber
Exceptions
Type Condition
OverflowException

The value is long.MinValue.

| Edit this page View Source

explicit operator decimal(XrplNumber)

Converts to decimal as TryToDecimal(out decimal) does.

Declaration
public static explicit operator decimal(XrplNumber number)
Parameters
Type Name Description
XrplNumber number
Returns
Type Description
decimal
Exceptions
Type Condition
OverflowException

The value is beyond the range of decimal.

| Edit this page View Source

operator >(XrplNumber, XrplNumber)

Greater than.

Declaration
public static bool operator >(XrplNumber left, XrplNumber right)
Parameters
Type Name Description
XrplNumber left
XrplNumber right
Returns
Type Description
bool
| Edit this page View Source

operator >=(XrplNumber, XrplNumber)

Greater than or equal.

Declaration
public static bool operator >=(XrplNumber left, XrplNumber right)
Parameters
Type Name Description
XrplNumber left
XrplNumber right
Returns
Type Description
bool
| Edit this page View Source

implicit operator XrplNumber(int)

Converts from int; always exact.

Declaration
public static implicit operator XrplNumber(int value)
Parameters
Type Name Description
int value
Returns
Type Description
XrplNumber
| Edit this page View Source

operator !=(XrplNumber, XrplNumber)

Inequality by value.

Declaration
public static bool operator !=(XrplNumber left, XrplNumber right)
Parameters
Type Name Description
XrplNumber left
XrplNumber right
Returns
Type Description
bool
| Edit this page View Source

operator <(XrplNumber, XrplNumber)

Less than.

Declaration
public static bool operator <(XrplNumber left, XrplNumber right)
Parameters
Type Name Description
XrplNumber left
XrplNumber right
Returns
Type Description
bool
| Edit this page View Source

operator <=(XrplNumber, XrplNumber)

Less than or equal.

Declaration
public static bool operator <=(XrplNumber left, XrplNumber right)
Parameters
Type Name Description
XrplNumber left
XrplNumber right
Returns
Type Description
bool
| Edit this page View Source

operator *(XrplNumber, XrplNumber)

x * y under Default.

Declaration
public static XrplNumber operator *(XrplNumber x, XrplNumber y)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
Returns
Type Description
XrplNumber
| Edit this page View Source

operator -(XrplNumber, XrplNumber)

x - y under Default.

Declaration
public static XrplNumber operator -(XrplNumber x, XrplNumber y)
Parameters
Type Name Description
XrplNumber x
XrplNumber y
Returns
Type Description
XrplNumber
| Edit this page View Source

operator -(XrplNumber)

Negation, which is always exact.

Declaration
public static XrplNumber operator -(XrplNumber x)
Parameters
Type Name Description
XrplNumber x
Returns
Type Description
XrplNumber
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX