Money module for python
Project description
real-money
Money class for Python 3.7 and higher. Forked from py-money. It has been forked as the original library seems unmantained, and some of the currencies have changed since the last update.
Unlike other Python money classes, this class enforces that all monetary amounts are represented with the correct number of decimal places for the currency. For example, 3.678 USD is invalid, as is 5.5 JPY.
Installation
Install the latest release with:
pip install real-money
If you want to have localised formatting, you should install with
Babel:
pip install real-money[babel]
Usage
A Money object can be created with an amount (specified as a string) and a
currency from the Currency class:
from money import Currency, Money
m = Money("9.95", Currency.GBP)
m
# GBP 9.95
If you try to use more decimals than declared for the currency in the ISO 4217 standard, it will throw an error:
m = Money("4.624", Currency.EUR)
# InvalidAmountError: Invalid amount for currency
m = Money("1200.5", Currency.JPY)
# InvalidAmountError: Invalid amount for currency
Money objects can also be created from and converted to sub units:
m = Money.from_sub_units(499, Currency.USD)
m
# USD 4.99
m.sub_units
# 499
Money is inmutable and supports most mathematical and logical operators:
m = Money("10.00", Currency.USD)
m / 2
# USD 5.00
m + Money("3.00", Currency.USD)
# USD 13.00
m > Money("5.55", Currency.USD)
# True
m < Money("5.55", Currency.USD)
# False
Money will automatically round to the correct number of decimal places for
the currency:
m = Money("9.95", Currency.EUR)
m * 0.15
# EUR 1.49
m = Money("10", Currency.JPY)
m / 3
# JPY 3
Money can be formatted for different locales (if you have Babel):
Money("3.24", Currency.USD).format("en_US")
# $3.24
Money("9.95", Currency.EUR).format("es_ES")
# 9,95 €
Money("7.36", Currency.EUR).format("en_UK")
# £7.36
Money("94", Currency.JPY).format("ja_JP")
# ¥94
Money does not support conversion between currencies. Mathematical and
logical operations between two money objects are only allowed if both objects
are of the same currency. Otherwise, an error will be thrown:
Money("1.25", Currency.USD) + Money("2", Currency.EUR)
# CurrencyMismatchError: Currencies must match
For more examples, check out the test file!
Acknowledgements
real-money is a fork of py-money.
Much of the code is borrowed from carlospalol/money. Much of the logic for handling foreign currencies is taken from sebastianbergmann/money. Money formatting is powered by Babel.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file real-money-0.1.0.tar.gz.
File metadata
- Download URL: real-money-0.1.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55af3eb41e1766455c22321a5e73c3a5e3b9cbd158f2084460fa12fb4d75aa4d
|
|
| MD5 |
f2814fa1355134d2c052395edbab32ed
|
|
| BLAKE2b-256 |
608f5bc2fec56710a5200a35f14b92146829578427c2cd43a6afad4d44265e4c
|
File details
Details for the file real_money-0.1.0-py3-none-any.whl.
File metadata
- Download URL: real_money-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
688bc131c24e10e19e378e8099a17bbbf6b44817a63ebc914568cf0aa6930d2d
|
|
| MD5 |
c87399ecf2d2472126d01ae6c20faaec
|
|
| BLAKE2b-256 |
24774597a746bac26dee5b05d91928499494c8486485688f6ab7e5895509df3b
|