Dinero is a library for working with monetary values in Python.
Project description
This project is inspired by the excellent dinero.js library.
Python Decimal instances are enough for basic monetary calculations, but when you face more complex use-cases they often show limitations and are not so intuitive to work with. Dinero provides a cleaner and easier to use API while still relying on the standard library. So it's still Decimal, but easier.
Install
pip install dinero
The problem
Using floats to do exact calculations in Python can be dangerous. When you try to find out how much 2.32 x 3 is, Python tells you it's 6.959999999999999. For some calculations, that’s fine. But if you are calculating a transaction involving money, that’s not what you want to see. Sure, you could round it off, but that's a little hacky.
>>> 2.32 * 3 == 6.96
False
>>> 2.32 * 3
6.959999999999999
You can read How to Count Money Exactly in Python to get a better idea.
Why Dinero?
A Dinero
object represent a specific monetary value. It comes with methods for creating, parsing, manipulating, testing and formatting.
>>> from dinero import Dinero
>>> from dinero.currencies import USD
>>>
>>> Dinero(2.32, USD) * 3 == Dinero(6.96. USD)
True
Currencies
Dinero give you access to more than 100 different currencies:
>>> from dinero.currencies import USD, EUR, GBP, INR, CLP
>>> amount = Dinero(2.32, EUR)
>>> amount.format(symbol=True, currency=True)
'€2.32 EUR'
>>>
>>> amount.raw_amount
Decimal('2.32')
More about currencies.
Operations
Operations can be performed between Dinero objects or between Dinero objects and numbers:
>>> total = Dinero(456.343567, USD) + 345.32 * 3
>>> print(total)
# 1,492.30
>>> total = (Dinero(345.32, USD).multiply(3)).add(456.343567)
>>> print(total)
# 1,492.30
More about operations.
Comparisons
Dinero objects can be compared to each other by using Python comparison operators:
>>> Dinero(100, EUR) == Dinero(100, EUR)
True
>>> Dinero(100, EUR).equals_to(Dinero(100, EUR))
True
More about comparisons.
Tools
Dinero give you access to some useful tools that allow you to perform common monetary calculations, like percentages, VAT, simple and compound interests, etc.
from dinero import Dinero
from dinero.currencies import USD
from dinero.tools import calculate_compound_interest
principal = Dinero("2000", USD)
total_interest = calculate_compound_interest(
principal=principal,
interest_rate=5,
duration=10,
compound_frequency=12,
)
total_interest.format(symbol=True, currency=True)
'$1,294.02 USD'
See all the available tools in the tools section.
Custom currencies
You can easily create custom currencies:
from dinero import Dinero
BTC = {
"code": "BTC",
"base": 10,
"exponent": 2,
"symbol": "₿",
}
Dinero(1000.5, BTC)
Dinero(amount=1000.5, currency={'code': 'BTC', 'base': 10, 'exponent': 2, 'symbol': '₿'})
More about custom currencies.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dinero-0.3.1.tar.gz
.
File metadata
- Download URL: dinero-0.3.1.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/5.15.153.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 231744ce90801e1a13df43eaeb6644998e437ebc122e7d0278f595150143e973 |
|
MD5 | e8f6fcf5b27590261cae6a76ea2b3f95 |
|
BLAKE2b-256 | e0a9d876fef5dd1a010278a89bfd81d68addf691800fa1086463a0e7412beca8 |
File details
Details for the file dinero-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: dinero-0.3.1-py3-none-any.whl
- Upload date:
- Size: 48.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/5.15.153.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0976c1f69fe6e6542084ef03346624520288805ce4b2c543d1a836cf375621a8 |
|
MD5 | d97ef96e337219dc4b7ec98b56fd188a |
|
BLAKE2b-256 | a6c63ff12ae118a1de79f53f44dc1f9732143406d28307829459dc093e5a5a01 |