Skip to main content

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

Project description

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 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 (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
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.

Currencies list was taken from https://github.com/sebastianbergmann/money.

Project details


Download files

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

Source Distribution

money-lib-1.0.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

money_lib-1.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file money-lib-1.0.0.tar.gz.

File metadata

  • Download URL: money-lib-1.0.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for money-lib-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a43e567fe228fa9866a94a6a952401929d024433bf876543bffcf6cba259412
MD5 b522500b79af9664acb852e52c296239
BLAKE2b-256 a5c250f8fb2fc0aa01e72d0a04be855fe783cc17083011277a190964bd9ae851

See more details on using hashes here.

File details

Details for the file money_lib-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: money_lib-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for money_lib-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db680b9ee801e7fc90080b8982284a0749ff4fa2ddadbe6163e227a6b45bb4da
MD5 c800b45b7a44b4d76c46a3fa6b54551c
BLAKE2b-256 d8dec0171b70eeb03922c4ba4f3e12e8ebc34d594731adf71d0d3a09592395c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page