Manage money in Shopify with a class that won't lose pennies during division
Latest commit be985d0 Aug 16, 2017 @jasonhl jasonhl committed on GitHub Merge pull request #89 from Shopify/comment_out_deprecation_message
Comment out deprecation message


Build Status codecov

money_column expects a decimal 8,3 database field.


  • Keeps value in decimal
  • Provides a Money class which encapsulates all information about an certain amount of money, such as its value and its currency.
  • Provides a Money::Currency class which encapsulates all information about a monetary unit.
  • Does NOT provides APIs for exchanging money from one currency to another.
  • wont lose pennies during division!
  • Money::NullCurrency for no currency support


gem 'money', github: 'shopify/money'


require 'money'

# 10.00 USD
money =, "USD")
money.subunits     #=> 1000
money.currency  #=>"USD")

# Comparisons, "USD") ==, "USD")   #=> true, "USD") ==, "USD")    #=> false, "USD") ==, "EUR")   #=> false, "USD") !=, "EUR")   #=> true

# Arithmetic, "USD") +, "USD") ==, "USD"), "USD") -, "USD") ==, "USD"), "USD") / 5                     ==, "USD"), "USD") * 5                     ==, "USD")

# Unit to subunit conversions
Money.from_subunits(500, "USD") ==, "USD")  # 5 USD
Money.from_subunits(5, "JPY") ==, "JPY")    # 5 JPY
Money.from_subunits(5000, "TND") ==, "TND") # 5 TND


Currencies are consistently represented as instances of Money::Currency. The most part of Money APIs allows you to supply either a String or a Money::Currency., "USD") ==,"USD")), "EUR").currency =="EUR")

A Money::Currency instance holds all the information about the currency, including the currency symbol, name and much more.

currency =, "USD").currency
currency.iso_code #=> "USD"     #=> "United States Dollar"
currency.to_s     #=> 'USD'
currency.symbol   #=> '$'
currency.disambiguate_symbol #=> 'US$'

Default Currency

By default Money defaults to Money::NullCurrency as its currency. This is a global variable that can be changed using:

Money.default_currency ="USD")

In web apps you might want to set the default currency on a per request basis. In Rails you can do this with an around action, for example:

class ApplicationController < ActionController::Base
  around_action :set_currency


  def set_currency
    Money.with_currency(current_shop.currency) { yield }

Currency Minor Units

The exponent of a money value is the number of digits after the decimal separator (which separates the major unit from the minor unit)."USD").minor_units  # => 2"JPY").minor_units  # => 0"MGA").minor_units  # => 1

Storing money

Since money internally uses BigDecimal it's logical to use a decimal column (or money for PostgreSQL) for your database. The money_column method can generate methods for use with ActiveRecord:

create_table :orders do |t|
  t.decimal :sub_total, precision: 20, scale: 3
  t.decimal :tax, precision: 20, scale: 3
  t.string :currency, limit: 3

class Order < ApplicationRecord
  money_column :sub_total, :tax

Because it called composed_of under the hood, you can use Money objects directly with the AR query interface :

Order.create(sub_total:, 'USD'), tax:, 'USD'))
Order.where(sub_total:, 'CAD'))

money_column uses an attribute called 'currency' by default. This can be overridden by supplying another column name, e.g.: currency_column: :other. If your currency is hardcoded and there is no column, set a falsey currency column and supply the currency code instead: money_column :price_usd, currency_column: false, currency: 'USD'

You can use multiple money_column calls to achieve the desired effects with currency on the model or attribute level.

There are no validations generated. You can add these for the specified money and currency attributes as you normally would for any other.

Contributing to money

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright (c) 2011 Shopify. See LICENSE.txt for further details.