Module Money::Arithmetic
In: lib/money/money/arithmetic.rb

Methods

%   *   +   -   /   <=>   ==   abs   div   divmod   eql?   modulo   nonzero?   remainder   zero?  

Public Instance methods

Synonym for +modulo+.

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

@return [Money]

@see modulo

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 a Money instance.

@example

  Money.new(100) * 2 #=> #<Money @cents=200>

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 @cents=200>

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 @cents=1>

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 @cents=10>
  Money.new(100) / Money.new(10) #=> 10.0

Checks whether two money objects have the same currency and the same amount. Checks against money objects with a different currency and checks against objects that do not respond to to_money will always return false.

@param [Money] other_money Value to compare with.

@return [Boolean]

@example

  Money.new(100) == Money.new(101) #=> false
  Money.new(100) == Money.new(100) #=> true

Return absolute value of self as a new Money object.

@return [Money]

@example

  Money.new(-100).abs #=> #<Money @cents=100>

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 #/

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 @cents=11>, #<Money @cents=1>]
  Money.new(100).divmod(Money.new(9)) #=> [11, #<Money @cents=1>]

Synonymous with +#==+.

@param [Money] other_money Value to compare with.

@return [Money]

@see #==

Equivalent to +self.divmod(val)[1]+

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

@return [Money]

@example

  Money.new(100).modulo(9)            #=> #<Money @cents=1>
  Money.new(100).modulo(Money.new(9)) #=> #<Money @cents=1>

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 @cents=100>
  Money.new(0).nonzero?   #=> nil

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 @cents=1>

Test if the money amount is zero.

@return [Boolean]

@example

  Money.new(100).zero? #=> false
  Money.new(0).zero?   #=> true

[Validate]