Sha256: 0fcd9e39c594d45957f46a36987ac0ae17dd1fe69e1e1d608d35c71adaaa29a4

Contents?: true

Size: 469 Bytes

Versions: 48

Compression:

Stored size: 469 Bytes

Contents

module Stellar

  # reopen class
  class Price

    MAX_PRECISION = (2**31) - 1

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

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

    def to_f
      n / d.to_f
    end

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

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

Version data entries

48 entries across 48 versions & 3 rubygems

Version Path
stellar-base-0.0.11 lib/stellar/price.rb
stellar-base-0.0.10 lib/stellar/price.rb
stellar-base-0.0.9 lib/stellar/price.rb
stellar-base-0.0.8 lib/stellar/price.rb
stellar-base-0.0.7 lib/stellar/price.rb
stellar-base-0.0.6 lib/stellar/price.rb
stellar-base-0.0.5 lib/stellar/price.rb
stellar-base-0.0.4 lib/stellar/price.rb