Skip to main content

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

Project description

money-lib

GitHub Workflow Status 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 Money
>>> from money.django.fields import MoneyField

>>> class Product(models.Model):
...     price = 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 carlospalol/money.

Django model field with multiple database columns by miracle2k.

Currency data and formatting is powered by Babel.

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-3.0.1.tar.gz (8.3 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-3.0.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: money-lib-3.0.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.12

File hashes

Hashes for money-lib-3.0.1.tar.gz
Algorithm Hash digest
SHA256 3a3c45127717d83cfd5b12a46ea404c5808eaf9726ee4b612f463f292f308af4
MD5 151675bcbdddc5a6f0f304556e4cf4ae
BLAKE2b-256 bd9f88fbd3b9983a80d1d9ddca26cb2315da609331f1f5c10192215c3010daa9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: money_lib-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.12

File hashes

Hashes for money_lib-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90b88d9015486747ea2060305a94b9eb2b2ca34cdd40d16c569249bc728a670e
MD5 bbd741321c9910d08817991bbc97cacd
BLAKE2b-256 0a2498ee4c9063284f2f085a9841df9498f40660724e485d66a29109a4a3d0a9

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