Class: Stellar::Price

Inherits:
XDR::Struct
  • Object
show all
Defined in:
lib/stellar/price.rb,
generated/stellar/price.rb

Overview

reopen class

Constant Summary collapse

MAX_PRECISION =
(2**31) - 1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_f(number) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/stellar/price.rb', line 8

def self.from_f(number)
  best_r = Util::ContinuedFraction.best_r(number, MAX_PRECISION)
  new({
    n: best_r.numerator,
    d: best_r.denominator
  })
end

Instance Method Details

#inspectObject



28
29
30
# File 'lib/stellar/price.rb', line 28

def inspect
  "#<Stellar::Price #{self}>"
end

#invertObject



16
17
18
# File 'lib/stellar/price.rb', line 16

def invert
  self.class.new(n:d,d:n)
end

#to_fObject



20
21
22
# File 'lib/stellar/price.rb', line 20

def to_f
  n / d.to_f
end

#to_sObject



24
25
26
# File 'lib/stellar/price.rb', line 24

def to_s
  "#{n} / #{d}"
end