money CI Releases License


A Crystal shard for dealing with money and currency conversion ported from RubyMoney.





Add this to your application's shard.yml:

    github: crystal-money/money

Install with shards install.


require "money"

# 10.00 USD
money =, "USD")
money.cents    # => 1000
money.currency # => Money::Currency.find("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_amount(5, "USD") ==,  "USD") # 5 USD
Money.from_amount(5, "JPY") ==,    "JPY") # 5 JPY
Money.from_amount(5, "TND") ==, "TND") # 5 TND

# Currency conversions
some_code_to_setup_exchange_rates, "USD").exchange_to("EUR") ==, "EUR")

# Formatting (see Formatting section for more options), "USD").format # => "$1.00", "GBP").format # => "£1.00", "EUR").format # => "€1.00"


Currencies are consistently represented as instances of Money::Currency. The most part of Money APIs allows you to supply either a String, Symbol or a Money::Currency., "USD") ==, Money::Currency.find("USD")), "EUR").currency == Money::Currency.find(:eur), "PLN").currency == Money::Currency[:pln]

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

currency =, "USD").currency
currency.code # => "USD" # => "United States Dollar"

To define a new Money::Currency use Money::Currency.register as shown below.

currency = Money::Currency.from_json({
  priority:            1,
  code:                "USD",
  iso_numeric:         840,
  name:                "United States Dollar",
  symbol:              "$",
  symbol_first:        true,
  subunit:             "Cent",
  subunit_to_unit:     100,
  decimal_mark:        ".",
  thousands_separator: ","


The pre-defined set of attributes includes:

All attributes except :code and :subunit_to_unit are optional. Some attributes, such as :symbol, are used by the Money class to print out a representation of the object. Other attributes, such as :name or :priority, exist to provide a basic API you can take advantage of to build your application.


The priority attribute is an arbitrary numerical value you can assign to the Money::Currency and use in sorting/grouping operation.

For instance, let's assume your web application needs to render a currency selector like the one available here. You can create a couple of custom methods to return the list of major currencies and all currencies as follows:

# Returns an array of currency id where priority < 10
def major_currencies(hash)

# Returns an array of all currency id
def all_currencies(hash)

# => ["usd", "eur", "gbp", "aud", "cad", "jpy"]

# => ["aed", "afn", "all", ...]

Default Currency

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

Money.default_currency = Money::Currency.find("CAD")
# or
Money.default_currency = :cad

Currency Exponent

The exponent of a money value is the number of digits after the decimal separator (which separates the major unit from the minor unit). See e.g. ISO 4217 for more information. You can find the exponent (as an Int32) by

Money::Currency.find("USD").exponent # => 2
Money::Currency.find("JPY").exponent # => 0
Money::Currency.find("MGA").exponent # => 1

Currency Lookup

To find a given currency by ISO 4217 numeric code (three digits) you can do

Money::Currency.find(&.iso_numeric.==(978)) # => #

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:["USD", "EUR"] = 1.24515["EUR", "USD"] = 0.803115, "USD").exchange_to("EUR") # =>, @currency="EUR"), "EUR").exchange_to("USD") # =>,  @currency="USD")

Comparison and arithmetic operations work as expected:, "USD")  1; 9.00 USD is smaller, "EUR") +, "EUR") # =>, @currency="EUR")["USD", "EUR"] = 0.5, "EUR") +, "USD") # =>, @currency="EUR")

Exchange rate stores

The default bank is initialized with an in-memory store for exchange rates.

Money.default_bank =

You can pass you own store implementation, ie. for storing and retrieving rates off a database, file, cache, etc.

Money.default_bank =

# Add to the underlying store["USD", "CAD"] = 0.9

# Retrieve from the underlying store["USD", "CAD"] # => 0.9

# Exchanging amounts just works, "USD").exchange_to("CAD") # => Money(@amount=9 @currency="CAD")

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

Money.default_bank =

You can also implement your own Bank to calculate exchanges differently. Different banks can share Stores.

Money.default_bank =

If you wish to disable automatic currency conversion to prevent arithmetic when currencies don't match:



By default, Money objects are rounded to the nearest cent and the additional precision is not preserved: # => "$2.35"

To round to the nearest cent (or anything more precise), you can use the Money#round method. # => "$2.35"

To retain the additional precision, you will also need to set Money.infinite_precision to true.

Money.infinite_precision = true          # => "$2.34567" # => "$2.3457"


There are several formatting rules for when Money#format is called. For more information, check out the formatting module source, or read the latest release's docs.

If you wish to format money according to the EU's Rules for expressing monetary units in either English, Irish, Latvian or Maltese:

money =, :gbp)               # => Money(@amount=1.23 @currency="GBP")
money.format(symbol: "#{money.currency} ") # => "GBP 1.23"


To parse a String containing amount with currency code or symbol you can do

Money.parse("$12.34")    # => Money(@amount=12.34, @currency="USD")
Money.parse("12.34 USD") # => Money(@amount=12.34, @currency="USD")



Thanks to all of the contributors for their awesome work on RubyMoney.