Price
Represents a price in Stellar, expressed as a fraction.
Prices in Stellar are represented as rational numbers (fractions) with a 32-bit numerator and 32-bit denominator. This representation allows for exact price calculations without floating-point rounding errors.
Usage
// Create a price directly from numerator and denominator
val price = Price(1, 2) // Represents 0.5
// Parse from a decimal string
val price2 = Price.fromString("1.5") // Approximates to fraction 3/2
// Convert to decimal string
println(price.toString()) // "0.5"Content copied to clipboard
Important Notes
When parsing from strings, the function approximates decimal values to fractions
Values that cannot be represented as fractions with 32-bit integers may lose precision
For exact pricing, prefer creating Price objects directly from known numerator/denominator
See also
Throws
if denominator is zero