Price
in package
Represents a price as a rational number (fraction) in the Stellar network
Prices in Stellar are represented as fractions with a numerator (n) and denominator (d) to avoid floating-point precision issues. This is commonly used in trading operations to specify exchange rates between assets.
For example, a price of 1.5 would be represented as n=3, d=2 (3/2).
Tags
Table of Contents
Methods
- __construct() : mixed
- Price constructor
- fromString() : Price|false
- Creates a Price from a decimal string
- fromXdr() : Price
- Creates a Price from XDR format
- getD() : int
- Gets the denominator of the price fraction
- getN() : int
- Gets the numerator of the price fraction
- toXdr() : XdrPrice
- Converts this price to XDR format
Methods
__construct()
Price constructor
public
__construct(int $n, int $d) : mixed
Parameters
- $n : int
-
The numerator of the price fraction
- $d : int
-
The denominator of the price fraction
fromString()
Creates a Price from a decimal string
public
static fromString(string $price) : Price|false
Converts a decimal price (e.g., "1.5") into a rational fraction using continued fraction approximation to maintain precision. The approximation uses a tolerance of 1.e-9, which may result in precision limitations for certain decimal values. The resulting numerator and denominator are constrained by 32-bit integer limits.
Parameters
- $price : string
-
The decimal price as a string
Return values
Price|false —The price object, or false if conversion fails
fromXdr()
Creates a Price from XDR format
public
static fromXdr(XdrPrice $xdrPrice) : Price
Parameters
- $xdrPrice : XdrPrice
-
The XDR encoded price
Return values
Price —The decoded price object
getD()
Gets the denominator of the price fraction
public
getD() : int
Return values
int —The denominator value
getN()
Gets the numerator of the price fraction
public
getN() : int
Return values
int —The numerator value
toXdr()
Converts this price to XDR format
public
toXdr() : XdrPrice
Return values
XdrPrice —The XDR representation of this price