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.currency import Currency
>>> currency = Currency('USD')
>>> 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.money 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 (Currency) 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
Decimal('6.831')
>>> money.amount
Decimal('6.83')
>>> money.currency
USD

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 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.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')

Credits

Most of the code is based of https://github.com/carlospalol/money.

Currency data and formatting is powered by Babel.

Metadata

Release files for money-lib 1.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 1.1.0
File Size Uploaded Provenance Actions
money-lib-1.1.0.tar.gz 5.1 kB Without provenance Details for money-lib-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 11.2 kB

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

Download URL money-lib-1.1.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ba1c918094e1a03ccc68a96dbf5ab8079ecca8bfd2ed152eec968999a81d6271
BLAKE2b-256 checksum
How to use checksums
a434d0343644bbcf45c6df7ea5c541c9ce92d8181ac32c3dfd0a9b2f6e548abf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

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

Download URL money_lib-1.1.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f81356bae6f2bf476734e2abce8aa33f31d0aa653b1a3fafea423f7d900ec6dd
BLAKE2b-256 checksum
How to use checksums
7512e6dfbda46ae6c30cc9889419be034d9408e8fe6a9445e2b7ed2038edf52b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

Security for money-lib 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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