Skip to main content

money-lib

PyPI - Version PyPI - Python Version PyPI - License

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

Installation

Install the latest release with:

pip install money-lib

Usage

A Currency object can be created with a currency_code (must be a string and valid ISO 4217 format: ^[A-Z]{3}$).

>>> from money import Currency
>>> currency = Currency('USD')
>>> currency
Currency('USD')

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 money import Money
>>> money = Money('7.37', 'USD')
>>> money
Money(Decimal('7.37'), 'USD')

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

>>> money = Money('6.831', 'USD')
>>> money.amount
Decimal('6.83')
>>> money.currency
Currency('USD')

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

>>> money = Money('5', 'USD')
>>> money / 2
Money(Decimal('2.5'), 'USD')
>>> money + Money('10', 'USD')
Money(Decimal('15'), 'USD')

All comparison and 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')
Money(Decimal('10.00'), 'USD')

Money supports formatting for different locales.

>>> money = Money('13.65', 'USD')
>>> money.format()
'$13.65'
>>> money.format('pt_PT')
'13,65 US$'

Currency exchange

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

A simple proof-of-concept backend money.exchange.SimpleBackend is included.

>>> from decimal import Decimal
>>> from money import Money, xrates

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

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

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

Django integration

Model fields usage:

>>> from django.db import models
>>> from money import fields, Money

>>> class Product(models.Model):
...     price = fields.MoneyField(max_digits=19, decimal_places=4, default=Money('10', 'USD'))

Model queries usage:

# MoneyField creates another field (MoneyField name + '_currency') to store the currency
>>> Product.objects.create(price=Money('10', 'USD'))
>>> Product.objects.create(price='10', price_currency='USD')

# Get all products where price is greater than 4 and the currency equals 'USD'
>>> product = Product.objects.filter(price__gt=4, price_currency='USD').first()
>>> product.price
Money(Decimal('10.0000'), 'USD')

Credits

Currency exchange support based on https://github.com/carlospalol/money.

Django model multiple database columns by miracle2k.

Currency data and formatting is powered by Babel.

Metadata

Release files for money-lib 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for money-lib 2.1.0
File Size Uploaded
money-lib-2.1.0.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for money-lib 2.1.0
File Interpreter ABI Platform
money_lib-2.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / money-lib-2.1.0.tar.gz

Download URL money-lib-2.1.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
59b2878fab7895efb3486a44976cef06e95fed3668343c847a5648e93b5bb48b
BLAKE2b-256 checksum
How to use checksums
eecc198d8b8403db4afaed13eaa4c214cf0b7a313dc3f6951359c89b599ae83f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release files / money_lib-2.1.0-py3-none-any.whl

Download URL money_lib-2.1.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
40e8d745e8ad5cb3bbd2fcc0d84a57c721cc0ee192bd8cb6baa9c0c2f09a25a2
BLAKE2b-256 checksum
How to use checksums
3f32518ad4aa0dd0167696f087bccb1add777d309f69d3310103572afcd6c96a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release history Release notifications | RSS feed

3.1.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

This release

2.1.0 This release

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page