module Money::Arithmetic

Public Instance Methods

%(val) click to toggle source

Synonym for #modulo.

@param [Money, Fixnum] val Number take modulo with.

@return [Money]

@see modulo

# File lib/money/money/arithmetic.rb, line 244
def %(val)
  modulo(val)
end
*(value) click to toggle source

Multiplies the monetary value with the given number and returns a new Money object with this monetary value and the same currency.

Note that you can't multiply a Money object by an other Money object.

@param [Numeric] value Number to multiply by.

@return [Money] The resulting money.

@raise [ArgumentError] If value is NOT a number.

@example

Money.new(100) * 2 #=> #<Money @fractional=200>
# File lib/money/money/arithmetic.rb, line 145
def *(value)
  if value.is_a? Numeric
    self.class.new(fractional * value, currency)
  else
    raise ArgumentError, "Can't multiply a #{self.class.name} by a #{value.class.name}'s value"
  end
end
+(other_money) click to toggle source

Returns a new Money object containing the sum of the two operands' monetary values. If other_money has a different currency then its monetary value is automatically exchanged to this object's currency using exchange_to.

@param [Money] other_money Other Money object to add.

@return [Money]

@example

Money.new(100) + Money.new(100) #=> #<Money @fractional=200>
# File lib/money/money/arithmetic.rb, line 106
def +(other_money)
  return self if other_money.zero?
  raise TypeError unless other_money.is_a?(Money)
  other_money = other_money.exchange_to(currency)
  self.class.new(fractional + other_money.fractional, currency)
end
-(other_money) click to toggle source

Returns a new Money object containing the difference between the two operands' monetary values. If other_money has a different currency then its monetary value is automatically exchanged to this object's currency using exchange_to.

@param [Money] other_money Other Money object to subtract.

@return [Money]

@example

Money.new(100) - Money.new(99) #=> #<Money @fractional=1>
# File lib/money/money/arithmetic.rb, line 124
def -(other_money)
  return self if other_money.zero?
  raise TypeError unless other_money.is_a?(Money)
  other_money = other_money.exchange_to(currency)
  self.class.new(fractional - other_money.fractional, currency)
end
-@() click to toggle source

Returns a money object with changed polarity.

@return [Money]

@example

- Money.new(100) #=> #<Money @fractional=-100>
# File lib/money/money/arithmetic.rb, line 23
def -@
  self.class.new(-fractional, currency)
end
/(value) click to toggle source

Divides the monetary value with the given number and returns a new Money object with this monetary value and the same currency. Can also divide by another Money object to get a ratio.

Money/Numeric returns Money. Money/Money returns Float.

@param [Money, Numeric] value Number to divide by.

@return [Money] The resulting money if you divide Money by a number. @return [Float] The resulting number if you divide Money by a Money.

@example

Money.new(100) / 10            #=> #<Money @fractional=10>
Money.new(100) / Money.new(10) #=> 10.0
# File lib/money/money/arithmetic.rb, line 168
def /(value)
  if value.is_a?(self.class)
    fractional / as_d(value.exchange_to(currency).fractional).to_f
  else
    self.class.new(fractional / as_d(value), currency)
  end
end
<=>(other_money) click to toggle source

Compares two Money objects. If money objects have a different currency it will attempt to convert the currency.

@param [Money] other_money Value to compare with.

@return [Fixnum]

@raise [TypeError] when other object is not Money

# File lib/money/money/arithmetic.rb, line 60
def <=>(other_money)
  return nil unless other_money.is_a?(Money)
  if fractional != 0 && other_money.fractional != 0 && currency != other_money.currency
    other_money = other_money.exchange_to(currency)
  end
  fractional <=> other_money.fractional
rescue Money::Bank::UnknownRate
  nil
end
abs() click to toggle source

Return absolute value of self as a new Money object.

@return [Money]

@example

Money.new(-100).abs #=> #<Money @fractional=100>
# File lib/money/money/arithmetic.rb, line 274
def abs
  self.class.new(fractional.abs, currency)
end
coerce(other) click to toggle source

Used to make Money instance handle the operations when arguments order is reversed @return [Array]

@example

2 * Money.new(10) #=> #<Money @fractional=20>
# File lib/money/money/arithmetic.rb, line 306
def coerce(other)
  [CoercedNumber.new(other), self]
end
div(value) click to toggle source

Synonym for #/.

@param [Money, Numeric] value Number to divide by.

@return [Money] The resulting money if you divide Money by a number. @return [Float] The resulting number if you divide Money by a Money.

@see #/

# File lib/money/money/arithmetic.rb, line 185
def div(value)
  self / value
end
divmod(val) click to toggle source

Divide money by money or fixnum and return array containing quotient and modulus.

@param [Money, Fixnum] val Number to divmod by.

@return [Array<Money,Money>,Array<Fixnum,Money>]

@example

Money.new(100).divmod(9)            #=> [#<Money @fractional=11>, #<Money @fractional=1>]
Money.new(100).divmod(Money.new(9)) #=> [11, #<Money @fractional=1>]
# File lib/money/money/arithmetic.rb, line 199
def divmod(val)
  if val.is_a?(Money)
    divmod_money(val)
  else
    divmod_other(val)
  end
end
eql?(other_money) click to toggle source

Checks whether two Money objects have the same currency and the same amount. If Money objects have a different currency it will only be true if the amounts are both zero. Checks against objects that are not Money or a subclass will always return false.

@param [Money] other_money Value to compare with.

@return [Boolean]

@example

Money.new(100).eql?(Money.new(101))                #=> false
Money.new(100).eql?(Money.new(100))                #=> true
Money.new(100, "USD").eql?(Money.new(100, "GBP"))  #=> false
Money.new(0, "USD").eql?(Money.new(0, "EUR"))      #=> true
Money.new(100).eql?("1.00")                        #=> false
# File lib/money/money/arithmetic.rb, line 42
def eql?(other_money)
  if other_money.is_a?(Money)
    (fractional == other_money.fractional && currency == other_money.currency) ||
      (fractional == 0 && other_money.fractional == 0)
  else
    false
  end
end
modulo(val) click to toggle source

Equivalent to +self.divmod(val)+

@param [Money, Fixnum] val Number take modulo with.

@return [Money]

@example

Money.new(100).modulo(9)            #=> #<Money @fractional=1>
Money.new(100).modulo(Money.new(9)) #=> #<Money @fractional=1>
# File lib/money/money/arithmetic.rb, line 233
def modulo(val)
  divmod(val)[1]
end
negative?() click to toggle source

Test if the amount is negative. Returns true if the money amount is less than 0, false otherwise.

@return [Boolean]

@example

Money.new(-1).negative? #=> true
Money.new(0).negative?  #=> false
Money.new(1).negative?  #=> false
# File lib/money/money/arithmetic.rb, line 92
def negative?
  fractional < 0
end
nonzero?() click to toggle source

Test if the money amount is non-zero. Returns this money object if it is non-zero, or nil otherwise, like +Numeric#nonzero?+.

@return [Money, nil]

@example

Money.new(100).nonzero? #=> #<Money @fractional=100>
Money.new(0).nonzero?   #=> nil
# File lib/money/money/arithmetic.rb, line 297
def nonzero?
  fractional != 0 ? self : nil
end
positive?() click to toggle source

Test if the amount is positive. Returns true if the money amount is greater than 0, false otherwise.

@return [Boolean]

@example

Money.new(1).positive?  #=> true
Money.new(0).positive?  #=> false
Money.new(-1).positive? #=> false
# File lib/money/money/arithmetic.rb, line 79
def positive?
  fractional > 0
end
remainder(val) click to toggle source

If different signs +self.modulo(val) - val+ otherwise +self.modulo(val)+

@param [Money, Fixnum] val Number to rake remainder with.

@return [Money]

@example

Money.new(100).remainder(9) #=> #<Money @fractional=1>
# File lib/money/money/arithmetic.rb, line 256
def remainder(val)
  if val.is_a?(Money) && currency != val.currency
    val = val.exchange_to(currency)
  end

  if (fractional < 0 && val < 0) || (fractional > 0 && val > 0)
    self.modulo(val)
  else
    self.modulo(val) - (val.is_a?(Money) ? val : self.class.new(val, currency))
  end
end
zero?() click to toggle source

Test if the money amount is zero.

@return [Boolean]

@example

Money.new(100).zero? #=> false
Money.new(0).zero?   #=> true
# File lib/money/money/arithmetic.rb, line 285
def zero?
  fractional == 0
end

Private Instance Methods

divmod_money(val) click to toggle source
# File lib/money/money/arithmetic.rb, line 207
def divmod_money(val)
  cents = val.exchange_to(currency).cents
  quotient, remainder = fractional.divmod(cents)
  [quotient, self.class.new(remainder, currency)]
end
divmod_other(val) click to toggle source
# File lib/money/money/arithmetic.rb, line 214
def divmod_other(val)
  if self.class.infinite_precision
    quotient, remainder = fractional.divmod(as_d(val))
    [self.class.new(quotient, currency), self.class.new(remainder, currency)]
  else
    [div(val), self.class.new(fractional.modulo(val), currency)]
  end
end