class Gnucash::Value

Represent a currency value as an integer so that integer math can be used for accuracy in computations.

Attributes

div[R]

@return [Integer] Divisor value.

val[R]

@return [Integer] The raw, undivided integer value.

Public Class Methods

new(val, div = 100) click to toggle source

Construct a Value object.

@param val [String, Integer]

Either a String in the form "1234/100" or an integer containing the
raw value.

@param div [Integer]

The divisor value to use (when +val+ is given as a Integer).
# File lib/gnucash/value.rb, line 28
def initialize(val, div = 100)
  if val.is_a?(String)
    if val =~ /^(-?\d+)\/(\d+)$/
      @val = $1.to_i
      @div = $2.to_i
    else
      raise "Unexpected value string: #{val.inspect}"
    end
  elsif val.is_a?(Integer)
    @val = val
    @div = div
  else
    raise "Unexpected value type: #{val.class}"
  end
end
zero() click to toggle source

Create a new Value object with value 0.

@return [Value] Zero value.

# File lib/gnucash/value.rb, line 17
def self.zero
  Value.new(0)
end

Public Instance Methods

*(other) click to toggle source

Multiply a Value object.

@param other [Numeric, Value] Multiplier.

@return [Numeric] Result of multiplication.

# File lib/gnucash/value.rb, line 88
def *(other)
  if other.is_a?(Value)
    other = other.to_f
  end
  if other.is_a?(Numeric)
    to_f * other
  else
    raise "Unexpected argument (#{other.inspect})"
  end
end
+(other) click to toggle source

Add to a Value object.

@param other [Value, Numeric]

@return [Value] Result of addition.

# File lib/gnucash/value.rb, line 49
def +(other)
  if other.is_a?(Value)
    lcm_div = @div.lcm(other.div)
    Value.new((@val * (lcm_div / @div)) + (other.val * (lcm_div / other.div)), lcm_div)
  elsif other.is_a?(Numeric)
    to_f + other
  else
    raise "Unexpected argument"
  end
end
-(other) click to toggle source

Subtract from a Value object.

@param other [Value, Numeric]

@return [Value] Result of subtraction.

# File lib/gnucash/value.rb, line 65
def -(other)
  if other.is_a?(Value)
    lcm_div = @div.lcm(other.div)
    Value.new((@val * (lcm_div / @div)) - (other.val * (lcm_div / other.div)), lcm_div)
  elsif other.is_a?(Numeric)
    to_f - other
  else
    raise "Unexpected argument"
  end
end
-@() click to toggle source

Negate a Value.

@return [Value] Result of negation.

# File lib/gnucash/value.rb, line 79
def -@
  Value.new(-@val, @div)
end
/(other) click to toggle source

Divide a Value object.

@param other [Numeric, Value] Divisor.

@return [Numeric] Result of division.

# File lib/gnucash/value.rb, line 104
def /(other)
  if other.is_a?(Value)
    other = other.to_f
  end
  if other.is_a?(Numeric)
    to_f / other
  else
    raise "Unexpected argument (#{other.inspect})"
  end
end
<=>(other) click to toggle source

Compare two Value objects.

@return [Integer] Comparison result.

# File lib/gnucash/value.rb, line 141
def <=>(other)
  lcm_div = @div.lcm(other.div)
  (@val * (lcm_div / @div)) <=> (other.val * (lcm_div / other.div))
end
==(other) click to toggle source

Test two Value objects for equality.

@return [Boolean]

Whether the two Value objects hold the same value.
# File lib/gnucash/value.rb, line 150
def ==(other)
  lcm_div = @div.lcm(other.div)
  (@val * (lcm_div / @div)) == (other.val * (lcm_div / other.div))
end
attributes() click to toggle source

Attributes available for inspection

@return [Array<Symbol>] Attributes used to build the inspection string @see Gnucash::Support::LightInspect

# File lib/gnucash/value.rb, line 159
def attributes
  %i[val div]
end
to_f() click to toggle source

Convert the Value to a Float.

@return [Float] Value of the value as a Float.

# File lib/gnucash/value.rb, line 125
def to_f
  @val / @div.to_f
end
to_r() click to toggle source

Convert the Value to a Rational.

@since 1.4.0

@return [Rational] Value of the value as a Rational.

# File lib/gnucash/value.rb, line 134
def to_r
  Rational(@val, @div)
end
to_s() click to toggle source

Represent the Value as a string (two decimal places).

@return [String] Representation of value.

# File lib/gnucash/value.rb, line 118
def to_s
  sprintf("%.02f", to_f)
end