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:



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

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

Parameters:

  • value (Numeric)

    Number to multiply by.

Returns:

  • (Money)

    The resulting money.

Raises:

  • (ArgumentError)

    If value is a Money instance.



136
137
138
139
140
141
142
# File 'lib/money/money/arithmetic.rb', line 136

def *(value)
  if value.is_a?(Money)
    raise ArgumentError, "Can't multiply a Money by a Money"
  else
    Money.new(fractional * 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 @fractional=200>

Parameters:

  • other_money (Money)

    Other Money object to add.

Returns:



95
96
97
98
99
100
101
# File 'lib/money/money/arithmetic.rb', line 95

def +(other_money)
  if currency == other_money.currency
    Money.new(fractional + other_money.fractional, other_money.currency)
  else
    Money.new(fractional + other_money.exchange_to(currency).fractional, 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 @fractional=1>

Parameters:

  • other_money (Money)

    Other Money object to subtract.

Returns:



114
115
116
117
118
119
120
# File 'lib/money/money/arithmetic.rb', line 114

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

#-@Money

Returns a money object with changed polarity.

Examples:

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

Returns:



10
11
12
# File 'lib/money/money/arithmetic.rb', line 10

def -@
  Money.new(-fractional, currency)
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 @fractional=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.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/money/money/arithmetic.rb', line 159

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

#<=>(other_money) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/money/money/arithmetic.rb', line 46

def <=>(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    if fractional == 0 || other_money.fractional == 0 || currency == other_money.currency
      fractional <=> other_money.fractional
    else
      fractional <=> other_money.exchange_to(currency).fractional
    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)


26
27
28
29
30
31
32
33
# File 'lib/money/money/arithmetic.rb', line 26

def ==(other_money)
  if other_money.respond_to?(:to_money)
    other_money = other_money.to_money
    fractional == other_money.fractional && 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 @fractional=100>

Returns:



260
261
262
# File 'lib/money/money/arithmetic.rb', line 260

def abs
  Money.new(self.fractional.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:



180
181
182
# File 'lib/money/money/arithmetic.rb', line 180

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

Parameters:

  • val (Money, Fixnum)

    Number to divmod by.

Returns:



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/money/money/arithmetic.rb', line 194

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

#eql?(other_money) ⇒ Money

Synonymous with #==.

Parameters:

  • other_money (Money)

    Value to compare with.

Returns:

See Also:



42
43
44
# File 'lib/money/money/arithmetic.rb', line 42

def eql?(other_money)
  self == other_money
end

#modulo(val) ⇒ Money

Equivalent to self.divmod(val)

Examples:

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

Parameters:

  • val (Money, Fixnum)

    Number take modulo with.

Returns:



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

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

#negative?Boolean

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

Examples:

Money.new(-1).negative? #=> true
Money.new(0).negative?  #=> false
Money.new(1).negative?  #=> false

Returns:

  • (Boolean)


81
82
83
# File 'lib/money/money/arithmetic.rb', line 81

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

Returns:



283
284
285
# File 'lib/money/money/arithmetic.rb', line 283

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

#positive?Boolean

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

Examples:

Money.new(1).positive?  #=> true
Money.new(0).positive?  #=> false
Money.new(-1).positive? #=> false

Returns:

  • (Boolean)


68
69
70
# File 'lib/money/money/arithmetic.rb', line 68

def positive?
  fractional > 0
end

#remainder(val) ⇒ Money

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

Examples:

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

Parameters:

  • val (Money, Fixnum)

    Number to rake remainder with.

Returns:



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/money/money/arithmetic.rb', line 242

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.fractional < 0
  b_sign = :neg if (b.is_a?(Money) and b.fractional < 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)


271
272
273
# File 'lib/money/money/arithmetic.rb', line 271

def zero?
  fractional == 0
end