Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Library for dealing with money and currency conversion

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 money.gemspec


This library aids one in handling money and different currencies. Features:

  • Provides a Money class which encapsulates all information about an certain amount of money, such as its value and its currency.

  • Represents monetary values as integers, in cents. This avoids floating point rounding errors.

  • Provides APIs for exchanging money from one currency to another.

  • Has the ability to parse a money string into a Money object.



Install stable releases with the following command:

gem install money

The development version (hosted on Github) can be installed with:

gem sources -a
gem install FooBarWidget-money



require 'money'

# 10.00 USD
money =, "USD")
money.cents     # => 1000
money.currency  # => "USD", "USD") ==, "USD")   # => true, "USD") ==, "USD")    # => false, "USD") ==, "EUR")   # => false

Currency Exchange

Exchanging money is performed through an exchange bank object. The default exchange bank object requires one to manually specify the exchange rate. Here's an example of how it works:

Money.add_rate("USD", "CAD", 1.24515)
Money.add_rate("CAD", "USD", 0.803115)

Money.us_dollar(100).exchange_to("CAD")  # =>, "CAD")
Money.ca_dollar(100).exchange_to("USD")  # =>, "USD")

Comparison and arithmetic operations work as expected:, "USD") <=>, "USD")   # => 1; 9.00 USD is smaller, "EUR") +, "EUR") ==, "EUR")

Money.add_rate("USD", "EUR", 0.5), "EUR") +, "USD") ==, "EUR")

There is nothing stopping you from creating bank objects which scrapes for the current rates or just returns rand(2):

Money.default_bank =

Ruby on Rails

Use the compose_of helper to let Active Record deal with embedding the money object in your models. The following example requires a cents and a currency field.

class ProductUnit < ActiveRecord::Base
  belongs_to :product
  composed_of :price, :class_name => "Money", :mapping => [%w(cents cents), %w(currency currency)]

    validate :cents_not_zero

    def cents_not_zero
      errors.add("cents", "cannot be zero or less") unless cents > 0

    validates_presence_of :sku, :currency
    validates_uniqueness_of :sku        

Default Currency

By default Money defaults to USD as its currency. This can be overwritten using

Money.default_currency = "CAD"

If you use Rails, then environment.rb is a very good place to put this.

Something went wrong with that request. Please try again.