Class: Money

Inherits:
Object
  • Object
show all
Extended by:
Constructors
Includes:
Comparable, Arithmetic
Defined in:
lib/money/money.rb,
lib/money/version.rb,
lib/money/currency.rb,
lib/money/bank/base.rb,
lib/money/currency/loader.rb,
lib/money/money/formatter.rb,
lib/money/money/allocation.rb,
lib/money/money/arithmetic.rb,
lib/money/money/constructors.rb,
lib/money/rates_store/memory.rb,
lib/money/currency/heuristics.rb,
lib/money/locale_backend/base.rb,
lib/money/locale_backend/i18n.rb,
lib/money/bank/single_currency.rb,
lib/money/money/locale_backend.rb,
lib/money/locale_backend/errors.rb,
lib/money/locale_backend/legacy.rb,
lib/money/bank/variable_exchange.rb,
lib/money/money/formatting_rules.rb,
lib/money/locale_backend/currency.rb

Overview

“Money is any object or record that is generally accepted as payment for goods and services and repayment of debts in a given socio-economic context or country.” -Wikipedia

An instance of Money represents an amount of a specific currency.

Money is a value object and should be treated as immutable.

Defined Under Namespace

Modules: Arithmetic, Bank, Constructors, LocaleBackend, RatesStore Classes: Allocation, Currency, Formatter, FormattingRules, UndefinedSmallestDenomination

Constant Summary collapse

VERSION =
'6.19.0'.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Constructors

ca_dollar, empty, euro, pound_sterling, us_dollar

Methods included from Arithmetic

#%, #*, #+, #-, #-@, #/, #<=>, #==, #abs, #coerce, #div, #divmod, #eql?, #modulo, #negative?, #nonzero?, #positive?, #remainder, #zero?

Constructor Details

#initialize(obj, currency = nil, options = {}) ⇒ Money

Creates a new Money object of value given in the fractional unit of the given currency.

Alternatively you can use the convenience methods like Money::Constructors#ca_dollar and Money::Constructors#us_dollar.

Examples:

Money.new(100)        #=> #<Money @fractional=100 @currency="USD">
Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">

Parameters:

  • obj (Object)

    Either the fractional value of the money, a Money object, or a currency. (If passed a currency as the first argument, a Money will be created in that currency with fractional value

    0.

  • currency (Currency, String, Symbol) (defaults to: nil)

    The currency format.

  • options (Hash) (defaults to: {})

    Optional settings for the new Money instance

  • [Money::Bank::*] (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/money/money.rb', line 350

def initialize(obj, currency = nil, options = {})
  # For backwards compatibility, if options is not a Hash, treat it as a bank parameter
  unless options.is_a?(Hash)
    options = { bank: options }
  end

  @fractional = as_d(obj.respond_to?(:fractional) ? obj.fractional : obj)
  @currency   = obj.respond_to?(:currency) ? obj.currency : Currency.wrap(currency)
  @currency ||= Money.default_currency
  @bank       = obj.respond_to?(:bank) ? obj.bank : options[:bank]
  @bank     ||= Money.default_bank

  # BigDecimal can be Infinity and NaN, money of that amount does not make sense
  raise ArgumentError, 'must be initialized with a finite value' unless @fractional.finite?
  raise Currency::NoCurrency, 'must provide a currency' if @currency.nil?
end

Class Attribute Details

.conversion_precisionInteger

Used to specify precision for converting Rational to BigDecimal

Returns:

  • (Integer)


149
# File 'lib/money/money.rb', line 149

attr_accessor :default_formatting_rules, :default_infinite_precision, :conversion_precision

.default_bankObject

Modified to support thread-local bank override



149
# File 'lib/money/money.rb', line 149

attr_accessor :default_formatting_rules, :default_infinite_precision, :conversion_precision

.default_formatting_rulesHash

Used to define a default hash of rules for every time Money#format is called. Rules provided on method call will be merged with the default ones. To overwrite a rule, just provide the intended value while calling format.

Examples:

Money.default_formatting_rules = { display_free: true }
Money.new(0, "USD").format                          # => "free"
Money.new(0, "USD").format(display_free: false)  # => "$0.00"

Returns:

  • (Hash)

See Also:



149
150
151
# File 'lib/money/money.rb', line 149

def default_formatting_rules
  @default_formatting_rules
end

.default_infinite_precisionBoolean

Returns Use this to enable infinite precision cents as the global default.

Returns:

  • (Boolean)

    Use this to enable infinite precision cents as the global default



149
# File 'lib/money/money.rb', line 149

attr_accessor :default_formatting_rules, :default_infinite_precision, :conversion_precision

.locale_backendObject

Returns the value of attribute locale_backend.



150
151
152
# File 'lib/money/money.rb', line 150

def locale_backend
  @locale_backend
end

.use_i18nBoolean

Used to disable i18n even if it’s used by other components of your app.

Returns:

  • (Boolean)


149
# File 'lib/money/money.rb', line 149

attr_accessor :default_formatting_rules, :default_infinite_precision, :conversion_precision

Instance Attribute Details

#bankObject (readonly)

Returns the value of attribute bank.



105
# File 'lib/money/money.rb', line 105

attr_reader :currency, :bank

#currencyCurrency (readonly)

Returns The money’s currency.

Returns:

  • (Currency)

    The money’s currency.



105
106
107
# File 'lib/money/money.rb', line 105

def currency
  @currency
end

#default_currencyMoney::Currency

Returns The default currency, which is used when Money.new is called without an explicit currency argument. The default value is Currency.new(“USD”). The value must be a valid Money::Currency instance.

Returns:

  • (Money::Currency)

    The default currency, which is used when Money.new is called without an explicit currency argument. The default value is Currency.new(“USD”). The value must be a valid Money::Currency instance.



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

def self.default_currency
  if @default_currency.nil?
    nil
  elsif @default_currency.respond_to?(:call)
    Money::Currency.new(@default_currency.call)
  else
    Money::Currency.new(@default_currency)
  end
end

Class Method Details

.add_rate(from_currency, to_currency, rate) ⇒ Numeric

Adds a new exchange rate to the default bank and return the rate.

Examples:

Money.add_rate("USD", "CAD", 1.25) #=> 1.25

Parameters:

  • from_currency (Currency, String, Symbol)

    Currency to exchange from.

  • to_currency (Currency, String, Symbol)

    Currency to exchange to.

  • rate (Numeric)

    Rate to exchange with.

Returns:

  • (Numeric)


289
290
291
# File 'lib/money/money.rb', line 289

def self.add_rate(from_currency, to_currency, rate)
  Money.default_bank.add_rate(from_currency, to_currency, rate)
end

.default_currencyObject



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

def self.default_currency
  if @default_currency.nil?
    nil
  elsif @default_currency.respond_to?(:call)
    Money::Currency.new(@default_currency.call)
  else
    Money::Currency.new(@default_currency)
  end
end

.default_currency=(currency) ⇒ Object



169
170
171
# File 'lib/money/money.rb', line 169

def self.default_currency=(currency)
  @default_currency = currency
end

.disallow_currency_conversion!Object

Sets the default bank to be a SingleCurrency bank that raises on currency exchange. Useful when apps operate in a single currency at a time.



295
296
297
# File 'lib/money/money.rb', line 295

def self.disallow_currency_conversion!
  self.default_bank = Bank::SingleCurrency.instance
end

.from_amount(amount, currency = default_currency, options = {}) ⇒ Money Also known as: from_dollars

Creates a new Money object of value given in the unit of the given currency.

Examples:

Money.from_amount(23.45, "USD") # => #<Money fractional:2345 currency:USD>
Money.from_amount(23.45, "JPY") # => #<Money fractional:23 currency:JPY>

Parameters:

  • amount (Numeric)

    The numerical value of the money.

  • currency (Currency, String, Symbol) (defaults to: default_currency)

    The currency format.

  • options (Hash) (defaults to: {})

    Optional settings for the new Money instance

  • [Money::Bank::*] (Hash)

    a customizable set of options

Returns:

Raises:

  • (ArgumentError)

See Also:



314
315
316
317
318
319
320
321
322
# File 'lib/money/money.rb', line 314

def self.from_amount(amount, currency = default_currency, options = {})
  raise ArgumentError, "'amount' must be numeric" unless Numeric === amount

  currency = Currency.wrap(currency) || Money.default_currency
  raise Currency::NoCurrency, 'must provide a currency' if currency.nil?

  value = amount.to_d * currency.subunit_to_unit
  new(value, currency, options)
end

.inherited(base) ⇒ Object



243
244
245
# File 'lib/money/money.rb', line 243

def self.inherited(base)
  base.setup_defaults
end

.rounding_modeBigDecimal::ROUND_MODE

Use this to return the rounding mode.

Returns:

  • (BigDecimal::ROUND_MODE)

    rounding mode



252
253
254
255
256
# File 'lib/money/money.rb', line 252

def self.rounding_mode
  return Thread.current[:money_rounding_mode] if Thread.current[:money_rounding_mode]

  @rounding_mode
end

.rounding_mode=(new_rounding_mode) ⇒ Object



209
210
211
# File 'lib/money/money.rb', line 209

def self.rounding_mode=(new_rounding_mode)
  @rounding_mode = new_rounding_mode
end

.setup_defaultsObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/money/money.rb', line 223

def self.setup_defaults
  # Set the default bank for creating new +Money+ objects.
  self.default_bank = Bank::VariableExchange.instance

  # Default to using i18n
  @use_i18n = true

  # Default to using legacy locale backend
  self.locale_backend = :legacy

  # Default to not using infinite precision cents
  self.default_infinite_precision = false

  # Default rounding mode toward the nearest neighbor; if the neighbors are equidistant, round away from zero
  self.rounding_mode = BigDecimal::ROUND_HALF_UP

  # Default the conversion of Rationals precision to 16
  self.conversion_precision = 16
end

.with_bank(bank) { ... } ⇒ Object

Thread-safe bank switching method Temporarily changes the default bank in the current thread only

Examples:

Money.with_bank(european_bank) do
  Money.new(100, "USD").exchange_to("EUR")
end

Parameters:

Yields:

  • The block within which the bank will be changed

Returns:

  • (Object)

    block results



196
197
198
199
200
201
202
# File 'lib/money/money.rb', line 196

def self.with_bank(bank)
  original_bank = Thread.current[:money_bank]
  Thread.current[:money_bank] = bank
  yield
ensure
  Thread.current[:money_bank] = original_bank
end

.with_rounding_mode(mode) { ... } ⇒ Object

Temporarily changes the rounding mode in a given block.

Examples:

fee = Money.with_rounding_mode(BigDecimal::ROUND_HALF_DOWN) do
  Money.new(1200) * BigDecimal('0.029')
end

Parameters:

  • mode (BigDecimal::ROUND_MODE)

Yields:

  • The block within which rounding mode will be changed. Its return value will also be the return value of the whole method.

Returns:

  • (Object)

    block results



271
272
273
274
275
276
277
# File 'lib/money/money.rb', line 271

def self.with_rounding_mode(mode)
  original_mode = Thread.current[:money_rounding_mode]
  Thread.current[:money_rounding_mode] = mode
  yield
ensure
  Thread.current[:money_rounding_mode] = original_mode
end

Instance Method Details

#allocate(parts) ⇒ Array<Money> Also known as: split

Splits a given amount in parts without losing pennies. The left-over pennies will be distributed round-robin amongst the parties. This means that parts listed first will likely receive more pennies than ones listed later.

Pass [2, 1, 1] as input to give twice as much to part1 as part2 or part3 which results in 50% of the cash to party1, 25% to part2, and 25% to part3. Passing a number instead of an array will split the amount evenly (without losing pennies when rounding).

Examples:

Money.new(5,   "USD").allocate([3, 7]) #=> [Money.new(2), Money.new(3)]
Money.new(100, "USD").allocate([1, 1, 1]) #=> [Money.new(34), Money.new(33), Money.new(33)]
Money.new(100, "USD").allocate(2) #=> [Money.new(50), Money.new(50)]
Money.new(100, "USD").allocate(3) #=> [Money.new(34), Money.new(33), Money.new(33)]

Parameters:

  • parts (Array<Numeric>, Numeric)

    how amount should be distributed to parts

Returns:



605
606
607
608
# File 'lib/money/money.rb', line 605

def allocate(parts)
  amounts = Money::Allocation.generate(fractional, parts, !Money.default_infinite_precision)
  amounts.map { |amount| dup_with(fractional: amount) }
end

#amountBigDecimal

Returns the numerical value of the money

Examples:

Money.new(1_00, "USD").amount    # => BigDecimal("1.00")

Returns:

  • (BigDecimal)

See Also:



396
397
398
# File 'lib/money/money.rb', line 396

def amount
  to_d
end

#as_ca_dollarMoney

Receive a money object with the same amount as the current Money object in Canadian dollar.

Examples:

n = Money.new(100, "USD").as_ca_dollar
n.currency #=> #<Money::Currency id: cad>

Returns:



571
572
573
# File 'lib/money/money.rb', line 571

def as_ca_dollar
  exchange_to("CAD")
end

#as_euroMoney

Receive a money object with the same amount as the current Money object in euro.

Examples:

n = Money.new(100, "USD").as_euro
n.currency #=> #<Money::Currency id: eur>

Returns:



583
584
585
# File 'lib/money/money.rb', line 583

def as_euro
  exchange_to("EUR")
end

#as_us_dollarMoney

Receive a money object with the same amount as the current Money object in United States dollar.

Examples:

n = Money.new(100, "CAD").as_us_dollar
n.currency #=> #<Money::Currency id: usd>

Returns:



559
560
561
# File 'lib/money/money.rb', line 559

def as_us_dollar
  exchange_to("USD")
end

#centsInteger, BigDecimal

Convenience method for fractional part of the amount. Synonym of #fractional

Returns:

  • (Integer)

    when infinite_precision is false

  • (BigDecimal)

    when infinite_precision is true

See Also:

  • infinite_precision


34
35
36
# File 'lib/money/money.rb', line 34

def cents
  fractional
end

#currency_as_stringString

Return string representation of currency object

Examples:

Money.new(100, :USD).currency_as_string #=> "USD"

Returns:

  • (String)


406
407
408
409
# File 'lib/money/money.rb', line 406

def currency_as_string
  warn "[DEPRECATION] `currency_as_string` is deprecated. Please use `.currency.to_s` instead."
  currency.to_s
end

#currency_as_string=(val) ⇒ Money::Currency

Set currency object using a string

Examples:

Money.new(100).currency_as_string("CAD") #=> #<Money::Currency id: cad>

Parameters:

  • val (String)

    The currency string.

Returns:



419
420
421
422
423
# File 'lib/money/money.rb', line 419

def currency_as_string=(val)
  warn "[DEPRECATION] `currency_as_string=` is deprecated - Money instances are immutable." \
    " Please use `with_currency` instead."
  @currency = Currency.wrap(val)
end

#decimal_markString

Returns a decimal mark according to the locale

Returns:

  • (String)


654
655
656
657
# File 'lib/money/money.rb', line 654

def decimal_mark
  (locale_backend && locale_backend.lookup(:decimal_mark, currency)) ||
    Money::Formatter::DEFAULTS[:decimal_mark]
end

#dollarsBigDecimal

Assuming using a currency using dollars: Returns the value of the money in dollars, instead of in the fractional unit cents.

Synonym of #amount

Examples:

Money.new(1_00, "USD").dollars   # => BigDecimal("1.00")

Returns:

  • (BigDecimal)

See Also:



382
383
384
# File 'lib/money/money.rb', line 382

def dollars
  amount
end

#dup_with(options = {}) ⇒ Object



659
660
661
662
663
664
665
# File 'lib/money/money.rb', line 659

def dup_with(options = {})
  self.class.new(
    options[:fractional] || fractional,
    options[:currency] || currency,
    bank: options[:bank] || bank
  )
end

#exchange_to(other_currency) {|n| ... } ⇒ Money

Receive the amount of this money object in another Currency.

Examples:

Money.new(2000, "USD").exchange_to("EUR")
Money.new(2000, "USD").exchange_to("EUR") {|x| x.round}
Money.new(2000, "USD").exchange_to(Currency.new("EUR"))

Parameters:

  • other_currency (Currency, String, Symbol)

    Currency to exchange to.

Yields:

  • (n)

    Optional block to use when rounding after exchanging one currency for another.

Yield Parameters:

  • n (Float)

    The resulting float after exchanging one currency for another.

Yield Returns:

  • (Integer)

Returns:



542
543
544
545
546
547
548
549
# File 'lib/money/money.rb', line 542

def exchange_to(other_currency, &rounding_method)
  other_currency = Currency.wrap(other_currency)
  if self.currency == other_currency
    self
  else
    @bank.exchange_with(self, other_currency, &rounding_method)
  end
end

#format(*rules) ⇒ String

Creates a formatted price string according to several rules.

Parameters:

Returns:

  • (String)


637
638
639
# File 'lib/money/money.rb', line 637

def format(*rules)
  Money::Formatter.new(self, *rules).to_s
end

#fractionalInteger, BigDecimal

The value of the monetary amount represented in the fractional or subunit of the currency.

For example, in the US dollar currency the fractional unit is cents, and there are 100 cents in one US dollar. So given the Money representation of one US dollar, the fractional interpretation is 100.

Another example is that of the Kuwaiti dinar. In this case the fractional unit is the fils and there 1000 fils to one Kuwaiti dinar. So given the Money representation of one Kuwaiti dinar, the fractional interpretation is 1000.

Returns:

  • (Integer)

    when infinite_precision is false

  • (BigDecimal)

    when infinite_precision is true

See Also:

  • infinite_precision


54
55
56
57
58
59
60
# File 'lib/money/money.rb', line 54

def fractional
  # Ensure we have a BigDecimal. If the Money object is created
  # from YAML, @fractional can end up being set to a Float.
  fractional = as_d(@fractional)

  return_value(fractional)
end

#hashInteger

Returns a Integer hash value based on the fractional and currency attributes in order to use functions like & (intersection), group_by, etc.

Examples:

Money.new(100).hash #=> 908351

Returns:

  • (Integer)


432
433
434
# File 'lib/money/money.rb', line 432

def hash
  [fractional.hash, currency.hash].hash
end

#inspectString

Common inspect function

Returns:

  • (String)


449
450
451
# File 'lib/money/money.rb', line 449

def inspect
  "#<#{self.class.name} fractional:#{fractional} currency:#{currency}>"
end

#round(rounding_mode = self.class.rounding_mode, rounding_precision = 0) ⇒ Money

Note:

This method is only useful when operating with infinite_precision turned on. Without infinite_precision values are rounded to the smallest unit of coinage automatically.

Round the monetary amount to smallest unit of coinage.

Examples:

Money.new(10.1, 'USD').round #=> Money.new(10, 'USD')

Returns:

See Also:



626
627
628
629
# File 'lib/money/money.rb', line 626

def round(rounding_mode = self.class.rounding_mode, rounding_precision = 0)
  rounded_amount = as_d(@fractional).round(rounding_precision, rounding_mode)
  dup_with(fractional: rounded_amount)
end

#round_to_nearest_cash_valueInteger, BigDecimal

Round a given amount of money to the nearest possible amount in cash value. For example, in Swiss franc (CHF), the smallest possible amount of cash value is CHF 0.05. Therefore, this method rounds CHF 0.07 to CHF 0.05, and CHF 0.08 to CHF 0.10.

Returns:

  • (Integer)

    when infinite_precision is false

  • (BigDecimal)

    when infinite_precision is true

See Also:

  • infinite_precision


71
72
73
74
75
76
# File 'lib/money/money.rb', line 71

def round_to_nearest_cash_value
  warn "[DEPRECATION] `round_to_nearest_cash_value` is deprecated - use " \
       "`to_nearest_cash_value.fractional` instead"

  to_nearest_cash_value.fractional
end

#symbolString

Uses Currency#symbol. If nil is returned, defaults to “¤”.

Examples:

Money.new(100, "USD").symbol #=> "$"

Returns:

  • (String)


442
443
444
# File 'lib/money/money.rb', line 442

def symbol
  currency.symbol || "¤"
end

#thousands_separatorString

Returns a thousands separator according to the locale

Returns:

  • (String)


645
646
647
648
# File 'lib/money/money.rb', line 645

def thousands_separator
  (locale_backend && locale_backend.lookup(:thousands_separator, currency)) ||
    Money::Formatter::DEFAULTS[:thousands_separator]
end

#to_dBigDecimal

Return the amount of money as a BigDecimal.

Examples:

Money.us_dollar(1_00).to_d #=> BigDecimal("1.00")

Returns:

  • (BigDecimal)


472
473
474
# File 'lib/money/money.rb', line 472

def to_d
  as_d(fractional) / as_d(currency.subunit_to_unit)
end

#to_fFloat

Return the amount of money as a float. Floating points cannot guarantee precision. Therefore, this function should only be used when you no longer need to represent currency or working with another system that requires floats.

Examples:

Money.us_dollar(100).to_f #=> 1.0

Returns:

  • (Float)


495
496
497
# File 'lib/money/money.rb', line 495

def to_f
  to_d.to_f
end

#to_iInteger

Return the amount of money as a Integer.

Examples:

Money.us_dollar(1_00).to_i #=> 1

Returns:

  • (Integer)


482
483
484
# File 'lib/money/money.rb', line 482

def to_i
  to_d.to_i
end

#to_money(given_currency = nil) ⇒ self

Conversion to self.

Returns:

  • (self)


517
518
519
520
521
522
523
524
# File 'lib/money/money.rb', line 517

def to_money(given_currency = nil)
  given_currency = Currency.wrap(given_currency)
  if given_currency.nil? || self.currency == given_currency
    self
  else
    exchange_to(given_currency)
  end
end

#to_nearest_cash_valueMoney

Round a given amount of money to the nearest possible money in cash value. For example, in Swiss franc (CHF), the smallest possible amount of cash value is CHF 0.05. Therefore, this method rounds CHF 0.07 to CHF 0.05, and CHF 0.08 to CHF 0.10.

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/money/money.rb', line 84

def to_nearest_cash_value
  unless self.currency.smallest_denomination
    raise UndefinedSmallestDenomination,
          "Smallest denomination of this currency is not defined"
  end

  fractional = as_d(@fractional)
  smallest_denomination = as_d(self.currency.smallest_denomination)
  rounded_value =
    (fractional / smallest_denomination)
      .round(0, self.class.rounding_mode) * smallest_denomination

  dup_with(fractional: return_value(rounded_value))
end

#to_sString

Returns the amount of money as a string.

Examples:

Money.ca_dollar(100).to_s #=> "1.00"

Returns:

  • (String)


459
460
461
462
463
464
# File 'lib/money/money.rb', line 459

def to_s
  format thousands_separator: '',
         no_cents_if_whole: currency.decimal_places == 0,
         symbol: false,
         ignore_defaults: true
end

#with_currency(new_currency) ⇒ self

Returns a new Money instance in a given currency leaving the amount intact and not performing currency conversion.

Parameters:

  • new_currency (Currency, String, Symbol)

    Currency of the new object.

Returns:

  • (self)


505
506
507
508
509
510
511
512
# File 'lib/money/money.rb', line 505

def with_currency(new_currency)
  new_currency = Currency.wrap(new_currency)
  if !new_currency || currency == new_currency
    self
  else
    dup_with(currency: new_currency)
  end
end