Sha256: 2f5505e48291a5590985eaab1c8f8bacd3e1b7032a7f2d548a3ff75b8008c4a4
Contents?: true
Size: 905 Bytes
Versions: 3
Compression:
Stored size: 905 Bytes
Contents
module Stellar # reopen class class Price MAX_PRECISION = (2**31) - 1 def self.from(number) number = BigDecimal(number, 0) if number.is_a?(String) number = number.to_r if number.respond_to?(:to_r) raise ArgumentError, "Couldn't convert #{number.class} to rational number" unless number.is_a?(Rational) best_r = number.rationalize(1.0e-7) if best_r.numerator > MAX_PRECISION || best_r.denominator > MAX_PRECISION raise ArgumentError, "Couldn't find valid price approximation for #{number}" end new(n: best_r.numerator, d: best_r.denominator) end def invert self.class.new(n: d, d: n) end def to_r Rational(n, d) end def to_d n.to_d / d end def to_f n.to_f / d end def to_s "#{n} / #{d}" end def inspect "#<Stellar::Price #{self}>" end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
stellar-base-0.32.0 | lib/stellar/price.rb |
stellar-base-0.31.0 | lib/stellar/price.rb |
stellar-base-0.30.0 | lib/stellar/price.rb |