Price

data class Price(val numerator: Int, val denominator: Int)

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"

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

Constructors

Link copied to clipboard
constructor(numerator: Int, denominator: Int)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The denominator of the price fraction

Link copied to clipboard

The numerator of the price fraction

Functions

Link copied to clipboard
open override fun toString(): String

Returns the decimal representation of this price.

Link copied to clipboard

Converts this Price to its XDR representation.