Skip to main content

Python 3 money lib with decimal precision and currency exchange support.

Project description


PyPI - Version PyPI - Python Version PyPI - License

Python 3 money lib with decimal precision and currency exchange support.


Install the latest release with:

pip install money-lib


A Money object can be created with an amount (can be any valid value in decimal.Decimal(value)) and a currency (can be a string or a Currency(code) object).

>>> from import Money
>>> money = Money('7.37', 'USD')
>>> money
USD 7.37

Money objects are immutable by convention and hashable. Once created, you can use read-only properties real (decimal.Decimal), amount (decimal.Decimal) and currency (str) to access its internal components. The real property returns the stored amount used for calculations and amount returns the amount rounded to the correct number of decimal places for the currency.

>>> money = Money('6.831', 'USD')
>>> money.real
>>> money.amount
>>> money.currency

Money emulates a numeric type and you can apply most arithmetic and comparison operators between money objects, integers (int) and decimal numbers (decimal.Decimal).

>>> money = Money('5', 'USD')
>>> money / 2
USD 2.50
>>> money + Money('10', 'USD')
USD 15.00

All arithmetic operators support automatic currency conversion as long as you have a currency exchange backend setup. The currency of the leftmost object has priority.

# Assuming the rate from USD to EUR is 2
>>> money = Money('7.50', 'USD')
>>> money + Money('5', 'EUR')
USD 10.00

Currency exchange

Currency exchange works by setting a backend class that implements the abstract base class Its API is exposed through money.xrates, along with xrates.backend and xrates.backend_name.

A simple proof-of-concept backend is included.

from decimal import Decimal
from import Money, xrates

xrates.backend = ''
xrates.base = 'USD'
xrates.setrate('AAA', Decimal('2'))
xrates.setrate('BBB', Decimal('8'))

a = Money(1, 'AAA')
b = Money(1, 'BBB')

assert'BBB') == Money('4', 'BBB')
assert'AAA') == Money('0.25', 'AAA')
assert a + b == Money('1.25', 'AAA')


Most of the code is based of

Currencies list was taken from

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for money-lib, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size money_lib-1.0.0-py3-none-any.whl (9.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size money-lib-1.0.0.tar.gz (10.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page