Class: Stellar::Price

Inherits:
Object
  • Object
show all
Defined in:
lib/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



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/stellar/price.rb', line 6

def self.from_f(number)
  best_r = number.to_r.rationalize(1.0e-7)

  if best_r.numerator > MAX_PRECISION || best_r.denominator > MAX_PRECISION
    raise ArgumentError("Couldn't find price approximation")
  end

  new({
    n: best_r.numerator,
    d: best_r.denominator
  })
end

Instance Method Details

#inspectObject



35
36
37
# File 'lib/stellar/price.rb', line 35

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

#invertObject



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

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

#to_dObject



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

def to_d
  n.to_d / d
end

#to_fObject



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

def to_f
  n.to_f / d
end

#to_sObject



31
32
33
# File 'lib/stellar/price.rb', line 31

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