Module: Money::Arithmetic

Included in:
Money
Defined in:
lib/money/money/arithmetic.rb

Instance Method Summary collapse

Instance Method Details

#%(val) ⇒ Money

Synonym for #modulo.

Parameters:

  • val (Money, Fixnum)

    Number take modulo with.

Returns:

See Also:



188
189
190
# File 'lib/money/money/arithmetic.rb', line 188

def %(val)
  self.modulo(val)
end

#*(value) ⇒ Money

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.

Examples:

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

Parameters:

  • value (Numeric)

    Number to multiply by.

Returns:

  • (Money)

    The resulting money.

Raises:

  • (ArgumentError)

    If value is a Money instance.



99
100
101
102
103
104
105
# File 'lib/money/money/arithmetic.rb', line 99

def *(value)
  if value.is_a?(Money)
    raise ArgumentError, "Can't multiply a Money by a Money"
  else
    Money.new(cents * value, currency)
  end
end

#+(other_money) ⇒ Money

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.

Examples:

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

Parameters:

  • other_money (Money)

    Other Money object to add.

Returns:



58
59
60
61
62
63
64
# File 'lib/money/money/arithmetic.rb', line 58

def +(other_money)
  if currency == other_money.currency
    Money.new(cents + other_money.cents, other_money.currency)
  else
    Money.new(cents + other_money.exchange_to(currency).cents, currency)
  end
end

#-(other_money) ⇒ Money

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.

Examples:

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

Parameters:

  • other_money (Money)

    Other Money object to subtract.

Returns:



77
78
79
80
81
82
83
# File 'lib/money/money/arithmetic.rb', line 77

def -(other_money)
  if currency == other_money.currency
    Money.new(cents - other_money.cents, other_money.currency)
  else
    Money.new(cents - other_money.exchange_to(currency).cents, currency)
  end
end

#/(value) ⇒ Money, Float

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.

Examples:

Money.new(100) / 10            #=> #<Money @cents=10>
Money.new(100) / Money.new(10) #=> 10.0

Parameters:

Returns:

  • (Money)

    The resulting money if you divide Money by a number.

  • (Float)

    The resulting number if you divide Money by a Money.



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/money/money/arithmetic.rb', line 122

def /(value)
  if value.is_a?(Money)
    if currency == value.currency
      (cents / BigDecimal.new(value.cents.to_s)).to_f
    else
      (cents / BigDecimal(value.exchange_to(currency).cents.to_s)).to_f
    end
  else
    Money.new(cents / value, currency)
  end
end

#<=>(other_money) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/money/money/arithmetic.rb', line 35

def <=>(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    if self.currency == other_money.currency
      cents <=> other_money.cents
    else
      cents <=> other_money.exchange_to(currency).cents
    end
  else
    raise ArgumentError, "Comparison of #{self.class} with #{other_money.inspect} failed"
  end
end

#==(other_money) ⇒ Boolean

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.

Examples:

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

Parameters:

  • other_money (Money)

    Value to compare with.

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/money/money/arithmetic.rb', line 15

def ==(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    cents == other_money.cents && self.currency == other_money.currency
  else
    false
  end
end

#absMoney

Return absolute value of self as a new Money object.

Examples:

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

Returns:



218
219
220
# File 'lib/money/money/arithmetic.rb', line 218

def abs
  Money.new(self.cents.abs, self.currency)
end

#div(value) ⇒ Money, Float

Synonym for #/.

Parameters:

Returns:

  • (Money)

    The resulting money if you divide Money by a number.

  • (Float)

    The resulting number if you divide Money by a Money.

See Also:



143
144
145
# File 'lib/money/money/arithmetic.rb', line 143

def div(value)
  self / value
end

#divmod(val) ⇒ Array<Money,Money>, Array<Fixnum,Money>

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

Examples:

Money.new(100).divmod(9)            #=> [#<Money @cents=11>, #<Money @cents=1>]
Money.new(100).divmod(Money.new(9)) #=> [11, #<Money @cents=1>]

Parameters:

  • val (Money, Fixnum)

    Number to divmod by.

Returns:



157
158
159
160
161
162
163
164
165
166
# File 'lib/money/money/arithmetic.rb', line 157

def divmod(val)
  if val.is_a?(Money)
    a = self.cents
    b = self.currency == val.currency ? val.cents : val.exchange_to(self.currency).cents
    q, m = a.divmod(b)
    return [q, Money.new(m, self.currency)]
  else
    return [self.div(val), Money.new(self.cents.modulo(val), self.currency)]
  end
end

#eql?(other_money) ⇒ Money

Synonymous with #==.

Parameters:

  • other_money (Money)

    Value to compare with.

Returns:

See Also:



31
32
33
# File 'lib/money/money/arithmetic.rb', line 31

def eql?(other_money)
  self == other_money
end

#modulo(val) ⇒ Money

Equivalent to self.divmod(val)

Examples:

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

Parameters:

  • val (Money, Fixnum)

    Number take modulo with.

Returns:



177
178
179
# File 'lib/money/money/arithmetic.rb', line 177

def modulo(val)
  self.divmod(val)[1]
end

#nonzero?Money?

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

Examples:

Money.new(100).nonzero? #=> #<Money @cents=100>
Money.new(0).nonzero?   #=> nil

Returns:



241
242
243
# File 'lib/money/money/arithmetic.rb', line 241

def nonzero?
  cents != 0 ? self : nil
end

#remainder(val) ⇒ Money

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

Examples:

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

Parameters:

  • val (Money, Fixnum)

    Number to rake remainder with.

Returns:



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/money/money/arithmetic.rb', line 200

def remainder(val)
  a, b = self, val
  b = b.exchange_to(a.currency) if b.is_a?(Money) and a.currency != b.currency

  a_sign, b_sign = :pos, :pos
  a_sign = :neg if a.cents < 0
  b_sign = :neg if (b.is_a?(Money) and b.cents < 0) or (b < 0)

  return a.modulo(b) if a_sign == b_sign
  a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b, a.currency))
end

#zero?Boolean

Test if the money amount is zero.

Examples:

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

Returns:

  • (Boolean)


229
230
231
# File 'lib/money/money/arithmetic.rb', line 229

def zero?
  cents == 0
end